Page 1 of 1

__Delete()

Posted: 20 Jan 2015, 12:01
by samardac
How __Delete() works? Can not understand. What is case when object delets?

Re: __Delete()

Posted: 20 Jan 2015, 13:26
by HotKeyIt
Whenever an Instance/object is deleted the function is called:

Code: Select all

Class a {
	__New(name:="AHK"){
		this.name:=name
	}
	__Delete(){
		MsgBox % "Instance '" this.name "' is being deleted."
	}
}
c:=new a("AutoHotkey")
c:=""

Re: __Delete()

Posted: 20 Jan 2015, 15:20
by samardac
Hm, I notice that it also start code from __Delete in this situation:

Code: Select all

Class a {
    __New(name:="AHK"){
        this.name:=name
    }
    __Delete(){
        MsgBox % "Instance '" this.name "' is being deleted."
    }
}
c:=new a("AutoHotkey")
Return
As you can see there is no c:="". That is why I asked about it.

Re: __Delete()

Posted: 20 Jan 2015, 15:50
by HotKeyIt
That is because AutoHotkey frees/releases all objects before it exits.

Re: __Delete()

Posted: 20 Jan 2015, 15:55
by samardac
So how to know fore sure when it will be exits?
I want to use it but can not understand in witch cases it initialises.

Re: __Delete()

Posted: 20 Jan 2015, 17:25
by HotKeyIt
You can use it whenever you need to clear resources, save settings ...
Simple example, here the allocated memory gets freed when object is released:

Code: Select all

Class Mem {
	__New(size:=100){
		if !this.ptr:=DllCall("GlobalAlloc","UInt",0,"UInt",size){
			MsgBox Error
			return ""
		}
	}
	__Delete(){
		DllCall("GlobalFree","PTR",this.ptr)
	}
}
c:=new Mem(1024)
MsgBox % "Memory allocated: " c.ptr
c:=""
MsgBox Memory freed

Re: __Delete()

Posted: 21 Jan 2015, 07:30
by samardac
HotKeyIt, I asked a bit different thing.
May be it is obvious for you but I'm newbie and it is not obvious for me.
So pleas tell what event should occur to initialize __Delete()?
No I can see at occurs when c:="" also it occurs when there is no c:="" as I shown in previous example.
So what are those events when __Delete() initializes apart from c:=""?

Re: __Delete()

Posted: 21 Jan 2015, 07:56
by MJs
HotKeyIt wrote:That is because AutoHotkey frees/releases all objects before it exits.
the event is closing the script, ending th program, terminating the applicatiln, cleaning; removing every object refrence.
the event is WM_QUIT. exit the app
does that help a bit?

Re: __Delete()

Posted: 21 Jan 2015, 08:01
by samardac
MJs, thank you, these are what I wanted,
Pleas tell what event is here?

Code: Select all

Class a {
    __New(name:="AHK"){
        this.name:=name
    }
    __Delete(){
        MsgBox % "Instance '" this.name "' is being deleted."
    }
}
c:=new a("AutoHotkey")
Return

Re: __Delete()

Posted: 21 Jan 2015, 08:19
by HotKeyIt
The event is ExitApp which happens after return because script is not persistent.
Btw. terminating the application will not cause the objects to be freed, only proper ExitApp/Reload or deleting the last reference to the object.

Re: __Delete()

Posted: 21 Jan 2015, 08:24
by samardac
HotKeyIt, thanx!
Now I understood, it was ExitApp because in script for example no hotkeys that will not allow script to Exit?
Witch command like hotkeys make script persistent?

Re: __Delete()

Posted: 21 Jan 2015, 08:41
by enthused
Please forgive me as I am a newbiw also but isn't #persistant make script persistant?

Re: __Delete()

Posted: 21 Jan 2015, 08:41
by MJs
#Persistent

Re: __Delete()

Posted: 21 Jan 2015, 08:43
by samardac
enthused, yep it is.
This is what is said in Help:
#Persistent
If this directive is present anywhere in the script, that script will stay running after the auto-execute section (top part of the script) completes. This is useful in cases where a script contains timers and/or custom menu items but not hotkeys, hotstrings, or any use of OnMessage() or Gui.

Re: __Delete()

Posted: 21 Jan 2015, 08:49
by MJs
samardac wrote:MJs, thank you, these are what I wanted,
Pleas tell what event is here?

Code: Select all

Class a {
    __New(name:="AHK"){
        this.name:=name
    }
    __Delete(){
        MsgBox % "Instance '" this.name "' is being deleted."
    }
}
c:=new a("AutoHotkey")
Return
you know it now: the event is closing the script.

Re: __Delete()

Posted: 21 Jan 2015, 08:52
by samardac
Thanx, to all, everything is clear now!

Re: __Delete()

Posted: 21 Jan 2015, 13:26
by jethrow
Just to be clear, the __Delete meta-function is called when an object is deleted ... that is, when the object loses all references. This would occur when the object variable is reassigned, the script terminates, or the object variable is cleared due to variable scope. For example, using your Class a from above, this will call the __Delete meta-function:

Code: Select all

#Persistent

MyFunc()

MyFunc() {
	c:=new a("AutoHotkey")
}
... but this will not (until the script terminates):

Code: Select all

#Persistent

c:=new a("AutoHotkey")
d:=c
c:=""