E.g.
Code: Select all
;q:: ;don't run this code (this will crash the script)
obj := {}
ObjRelease(&obj)
ExitApp ;script crashes
;w:: ;don't run this code (this will crash the script)
obj := {}
;ref count: 1
ObjRelease(&obj)
;ref count: 0
MsgBox
Reload ;script crashes
return
Code: Select all
w:: ;2 ways to get an object's reference count
obj := {}
vRefCount1 := NumGet(&obj + A_PtrSize) ;note: this method is undocumented and thus not guaranteed to work in all future versions of AHK
vRefCount2 := ObjAddRef(&obj)-1, ObjRelease(&obj)
obj := ""
MsgBox, % vRefCount1 " " vRefCount2
return
q:: ;tests involving object reference counts
obj := {}
MsgBox, % NumGet(&obj + A_PtrSize) ;ref count: 1
address := &obj
MsgBox, % NumGet(&obj + A_PtrSize) ;ref count: 1
address2 := Object(obj)
MsgBox, % NumGet(&obj + A_PtrSize) ;ref count: 2
obj2 := Object(&obj)
MsgBox, % NumGet(&obj + A_PtrSize) ;ref count: 3
obj3 := obj
MsgBox, % NumGet(&obj + A_PtrSize) ;ref count: 4
obj3 := obj ;attempt to do the same thing as in earlier line
MsgBox, % NumGet(&obj + A_PtrSize) ;ref count: 4
obj4 := obj
MsgBox, % NumGet(&obj + A_PtrSize) ;ref count: 5
obj := obj2 := obj3 := obj4 := ""
return
Links:
ObjAddRef() / ObjRelease()
https://autohotkey.com/docs/commands/ObjAddRef.htm
Objects
https://autohotkey.com/docs/Objects.htm ... e_Counting
ObjRelease needed when using ComObjValue+ObjAddRef? - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?t=4839
[ComObjEnwrap, ComObjUnwrap]
ComObjActive()
https://autohotkey.com/docs/commands/ComObjActive.htm
Btw is it so important to always clear each object via obj := "", when no longer needed, or is it OK to leave a few objects (with a small number of keys) to exist that aren't needed e.g. created from using StrSplit. [EDIT:] I will always try to clear each object when it's no longer needed, but is the overhead so bad, and is the number of objects or the size of objects more of a problem?
[EDIT:]
- Is there anything the script itself should dispose of before it closes, or does AHK tidy up *everything* for you.
- Does AHK tidy up i.e. clear any existing objects when the script is closed, or are any remnants of objects left behind?