Can I influence the order of destructor calls? One class depending on another without inheritance

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Steve06
Posts: 14
Joined: 19 Dec 2018, 09:21

Can I influence the order of destructor calls? One class depending on another without inheritance

15 Apr 2020, 05:29

My AHK class "AppState" relies on another well-known AHK class, AutoHotkey-JSON [JSON.ahk], as found https://github.com/cocobelgica/AutoHotkey-JSON .

More precisely, when my program quits, the state/settings of my app are to be written to a JSON file. Hence, in the destructor, I need to rely on JSON.ahk's JSON.Dump() to stringify my object.

Code: Select all

class AppState {
	state_data:={}		;the object holding the state data
	__New(filename) {  	;initialize AppState from a file using JSON.Load() to convert JSON string data to a AHK object
	...
	}
	...
	__Delete() { 		;destructor. Serializes app state using JSON.Dump() to stringify state_data object
	...
	}
}

state:=AppState("state.json")

GuiClose:
	state:={}	;destroys object explicitly and calls destructor; but can we do without?
ExitApp
This works smoothly when the state object is destroyed explicitly as in the last GuiClose bit of my code. But if I leave this out [i.e., no "state:={}"] and let AHK destroy the object for me upon quitting the program, the JSON.Dump() call in my destructor fails to work, 0 bytes are written to file, as apparently the JSON.ahk class has already been destroyed/made unanvailable. (How) Would it be possible to have JSON.ahk live long enough for my destructor call to succeed?
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Can I influence the order of destructor calls? One class depending on another without inheritance

15 Apr 2020, 06:11

Steve06 wrote:
15 Apr 2020, 05:29
as apparently the JSON.ahk class has already been destroyed/made unanvailable
how do u figure this is the case?
the following works just as expected:

Code: Select all

#Include <JSON>

class AppState
{
	state_data := {some: "data"}

	__Delete() {
		MsgBox % JSON.Dump(this.state_data)
	}
}

AS := new AppState()

Gui Add, Button, , Close
Gui Show
Return

ButtonClose:
	ExitApp
Steve06
Posts: 14
Joined: 19 Dec 2018, 09:21

Re: Can I influence the order of destructor calls? One class depending on another without inheritance

15 Apr 2020, 08:35

Thanks swagfag,
I tested your code snippet and can confirm it works as expected. It would appear the destruction order is fine.

After further tinkering with my code I have been able to narrow down the issue:

For me, on my machine running AHK 1.1.32.00, your code breaks for some variable names for the AppState instance, while for other names it works fine. It sounds crazy, doesn't it?

So, if instead of "AS", I use any of the variable names "state", "mystate", "mystateobject", "m1" the MsgBox displays an empty string!

On the other hand, "A", "AT", "abcde", for example, work fine. To me it appears purely random, I wasn't able to discern a pattern.

Example code not working:

Code: Select all

#Include <JSON>

class AppState
{
	state_data := {some: "data"}

	__Delete() {
		MsgBox % JSON.Dump(this.state_data)
	}
}

m1 := new AppState()

Gui Add, Button, , Close
Gui Show
Return

ButtonClose:
	ExitApp
Can you please check?
just me
Posts: 9553
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Can I influence the order of destructor calls? One class depending on another without inheritance

15 Apr 2020, 10:14

Seems to depend on the alphabetical order off class / instance names.

Code: Select all

JSOM := new AppState() ; works
JSOO := new AppState() ; doesn't
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Can I influence the order of destructor calls? One class depending on another without inheritance

15 Apr 2020, 10:16

cleanup upon exit is done alphabetically, so if ur varname is > "j", the JSON class is gone by then.
u can hold a reference to the class in urs:

Code: Select all

class AppState
{
    static JSON := JSON
	state_data := {some: "data"}

	__Delete() {
		MsgBox % this.JSON.Dump(this.state_data)
	}
}
Last edited by swagfag on 15 Apr 2020, 12:39, edited 1 time in total.
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: Can I influence the order of destructor calls? One class depending on another without inheritance

15 Apr 2020, 12:34

the order of releasing is not documented and you should not rely on any results from some experiments. I'd recommend you use :arrow: onexit to do clean up when the program is about to terminate. Edit, also swagfag's example is fine. Edit 2, static this.JSON := JSON :arrow: static JSON := JSON
Cheers.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: ntepa and 159 guests