Safe to link two objects together? Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
User avatar
Coiler
Posts: 75
Joined: 29 Nov 2020, 09:06

Safe to link two objects together?

Post by Coiler » 13 Dec 2020, 23:49

Since AutoHotKey releases variables based on reference, would it cause problems to link two class instance objects together, sort of like a linked list?

Something simple like this:

Code: Select all

a := MyClass.New()
b := MyClass.New()

a.Link := b
b.Link := a
Will this make it impossible for the references to be released until I manually assign the links to nothing at some point? If both objects are created and linked inside of a function as local variables, will both objects be released when it exits while still linked together this way?

Sorry if this doesn't make sense. I've been awake too long.

swagfag
Posts: 4126
Joined: 11 Jan 2017, 17:59

Re: Safe to link two objects together?  Topic is solved

Post by swagfag » 14 Dec 2020, 02:46

If both objects are created and linked inside of a function as local variables, will both objects be released when it exits while still linked together this way?
no they wont be, and if u for some reason lose ur only handle to those objects, u will have leaked them. there is more(although not much) on circular references in the documentation, somewhere

User avatar
Coiler
Posts: 75
Joined: 29 Nov 2020, 09:06

Re: Safe to link two objects together?

Post by Coiler » 14 Dec 2020, 07:51

Thanks, I appreciate the info.

Is there any clever way around this problem? Does it resolve the issue if we nullify the references in the destructors?

My object list is actually global, but I would still prefer not to use a design that causes issues like this. Thanks again.

Edit:
Pretty sure I can answer my own question here after thinking about it. The destructor would never be called, because the object still has a reference count above zero. The only way I can think of to resolve this is to create a "clear object" function and manually clear them all after finishing with them?

If anyone else finds this question through search, the best way to reference objects in a global array (that doesn't have its elements removed often) is by using the array's indices. Simply use the array index/location/iterator as the reference to each object. It requires the working code to have access to the root array object, and also inflicts pain upon the user if they try to remove elements from the array, or add new elements into the middle of the array (both events throws off all indices higher than the removed/added element). There are some other issues as well. But for global arrays that remain constant size, or at least don't get things removed or added to the middle, indices are perfect for referencing.

Post Reply

Return to “Ask For Help”