__Delete()

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
samardac
Posts: 212
Joined: 30 Nov 2014, 13:36

__Delete()

20 Jan 2015, 12:01

How __Delete() works? Can not understand. What is case when object delets?
HotKeyIt
Posts: 2364
Joined: 29 Sep 2013, 18:35
Contact:

Re: __Delete()

20 Jan 2015, 13:26

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:=""
samardac
Posts: 212
Joined: 30 Nov 2014, 13:36

Re: __Delete()

20 Jan 2015, 15:20

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.
HotKeyIt
Posts: 2364
Joined: 29 Sep 2013, 18:35
Contact:

Re: __Delete()

20 Jan 2015, 15:50

That is because AutoHotkey frees/releases all objects before it exits.
samardac
Posts: 212
Joined: 30 Nov 2014, 13:36

Re: __Delete()

20 Jan 2015, 15:55

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.
HotKeyIt
Posts: 2364
Joined: 29 Sep 2013, 18:35
Contact:

Re: __Delete()

20 Jan 2015, 17:25

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
samardac
Posts: 212
Joined: 30 Nov 2014, 13:36

Re: __Delete()

21 Jan 2015, 07:30

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:=""?
MJs
Posts: 454
Joined: 23 Sep 2014, 03:29

Re: __Delete()

21 Jan 2015, 07:56

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?
samardac
Posts: 212
Joined: 30 Nov 2014, 13:36

Re: __Delete()

21 Jan 2015, 08:01

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
HotKeyIt
Posts: 2364
Joined: 29 Sep 2013, 18:35
Contact:

Re: __Delete()

21 Jan 2015, 08:19

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.
samardac
Posts: 212
Joined: 30 Nov 2014, 13:36

Re: __Delete()

21 Jan 2015, 08:24

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?
enthused
Posts: 94
Joined: 27 Dec 2014, 03:28

Re: __Delete()

21 Jan 2015, 08:41

Please forgive me as I am a newbiw also but isn't #persistant make script persistant?
MJs
Posts: 454
Joined: 23 Sep 2014, 03:29

Re: __Delete()

21 Jan 2015, 08:41

#Persistent
samardac
Posts: 212
Joined: 30 Nov 2014, 13:36

Re: __Delete()

21 Jan 2015, 08:43

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.
MJs
Posts: 454
Joined: 23 Sep 2014, 03:29

Re: __Delete()

21 Jan 2015, 08:49

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.
samardac
Posts: 212
Joined: 30 Nov 2014, 13:36

Re: __Delete()

21 Jan 2015, 08:52

Thanx, to all, everything is clear now!
User avatar
jethrow
Posts: 188
Joined: 30 Sep 2013, 19:52
Location: Iowa

Re: __Delete()

21 Jan 2015, 13:26

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:=""

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Chunjee and 138 guests