objects: clearing objects

Get help with using AutoHotkey and its commands and hotkeys
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

objects: clearing objects

20 Jul 2017, 15:05

In these two examples, I do a task twice, with/without assigning an object. If I *don't* assign an object, does the object still exist, but without a name, and without being cleared?

Also, in the Explorer example, is it OK just to replace one object with another, or should I really be clearing the object the first, before I assign the same name to a different object.

Thanks for reading.

Code: Select all

q::
MsgBox, % (oArray := StrSplit("a,b,c,d,e", ",")).Length()
MsgBox, % StrSplit("f,g,h,i,j", ",").Length()
MsgBox, % IsObject(oArray)
oArray := ""
return

w::
WinGetClass, vWinClass, A
if !(vWinClass = "CabinetWClass") && !(vWinClass = "ExploreWClass")
	return

for oWin in ComObjCreate("Shell.Application").Windows
	if (oWin.HWND = hWnd)
		break
vDir := oWin.Document.Folder.Self.Path

for oWin in oWindows := ComObjCreate("Shell.Application").Windows
	if (oWin.HWND = hWnd)
		break
vDir := oWin.Document.Folder.Self.Path

MsgBox, % IsObject(oWin) " " IsObject(oWindows)
oWindows := oWin := ""
return
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

Re: objects: clearing objects

20 Jul 2017, 15:48

As I understand it, AHK implements reference counting. So when you store a reference to an object (in a variable or in another object) the reference count is increased. When you clear the contents of that variable/object, this decrements the reference count. When the reference count reaches zero AHK releases the object.
If I *don't* assign an object, does the object still exist, but without a name, and without being cleared?
To my understanding, the object would be released when the expression finishes evaluating.
Also, in the Explorer example, is it OK just to replace one object with another, or should I really be clearing the object the first, before I assign the same name to a different object.
I think both have the same effect.
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: objects: clearing objects

26 Jul 2017, 14:54

For functions, do local objects (objects defined within the body of the function) get cleared when the function ends?

@kon: Thanks.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
HotKeyIt
Posts: 2364
Joined: 29 Sep 2013, 18:35
Contact:

Re: objects: clearing objects

26 Jul 2017, 15:56

jeeswg wrote:For functions, do local objects (objects defined within the body of the function) get cleared when the function ends?

@kon: Thanks.
If they are not assigned to a variable or another object they are cleared directly.
If assigned to local variable, before function exits local vars get cleared and objects gets released/deleted (unless you return the object(s)).

Return to “Ask For Help”

Who is online

Users browsing this forum: andrict05, Bing [Bot], ECASNIPER118, Google [Bot], mikeyww, Rohwedder, tatagi, teadrinker and 48 guests