If a script retrieves a pointer to an object, it should always be released at some point.
Usually it is not necessary if you are exiting the script, but it is still good practice.
If window, pwb, Ret and title are pointers to objects, they should be released. If Ret is actually a comma-delimited list of values,
each value should be released, but only those values that are pointers to objects. COM_Release(Ret) will not work unless there is only one pointer (and no comma.)
However, since Ret is returned to the caller, it is the caller's responsibility to release any object pointers it may contain. If the function were to release these before returning them to the caller, it would be unsafe for the caller to access them. In any case, only the caller (who supplied VarNames_to_Return) knows which values are pointers and which are not. *
If title is text (not an object pointer), COM_Release(title) is invalid - COM.ahk has already converted the value to an AutoHotkey string, so the memory used by title will be freed when the function returns or you do title:="".
Releasing pwb will only release that object pointer, not any other objects retrieved through pwb. (This may not apply to some flawed implementations.)
Also note that lines such as the following are
not recommended:
COM_Invoke(psw := COM_Invoke([color=red]COM_CreateObject("Shell.Application")[/color], "Windows"), "Count")
window:= COM_Invoke([color=red]COM_Invoke(pwb, "Document")[/color], "parentWindow")
The red code retrieves object pointers which are lost, with no way to release them. This is why functions like COM_InvokeDeep and ez_Invoke were invented.
Btw, I must question the usefulness of this:
If (InStr([color=red]title[/color][color=darkred]:=COM_Invoke([/color][color=indigo]pwb := COM_Invoke(psw, "Item", A_Index-1)[/color][color=darkred], "LocationName")[/color], [color=red]title[/color], 0)) && ...
Since Haystack and Needle are the same, it will always evaluate to true. (This also depends on the order in which AutoHotkey evaluates parameters, which I believe is undocumented.)
One final tip: Refer to the documentation for the object you are using if you are unsure whether a returned value should be released. Objects should be released; strings, integers, etc. should not. For instance, compare the following properties:
Gets the automation object of the active document, if any.
Syntax
[ oAutomation = ] object.Document
Possible Values
oAutomation - Object that receives the automation object.
Retrieves the path or title of the resource that is currently displayed.
Syntax
[ sLocationName = ] object.LocationName
Possible Values
sLocationName - String that receives the location name. The caller frees this buffer with SysFreeString.
(COM_Invoke calls SysFreeString when it converts string values from unicode to ansi, so you do not need to.)
* It would be possible to extend COM_Invoke to return type information, but I guess that's not relevant.