Page 2 of 2

Re: removing images

Posted: Mon Oct 25, 2021 5:39 pm
by GVovkiv
milon wrote: Mon Oct 25, 2021 5:11 pm Note that after calling any :release() function, it's also best to set that variable to nil.
I still don't get why after :release better to set variable to nil
Can someone explain me why?

Re: removing images

Posted: Mon Oct 25, 2021 6:24 pm
by pgimeno
Yeah, thanks milon.
GVovkiv wrote: Mon Oct 25, 2021 5:39 pm I still don't get why after :release better to set variable to nil
Can someone explain me why?
Because the object is no longer usable, and trying to use it may crash your program; it also prevents the Lua-side object from being released.

Re: removing images

Posted: Mon Oct 25, 2021 6:39 pm
by GVovkiv
pgimeno wrote: Mon Oct 25, 2021 6:24 pm Yeah, thanks milon.
Because the object is no longer usable, and trying to use it may crash your program; it also prevents the Lua-side object from being released.
Well, that makes sense, thanks

Re: removing images

Posted: Mon Oct 25, 2021 9:04 pm
by ReFreezed
pgimeno wrote: Mon Oct 25, 2021 6:24 pm Because the object is no longer usable, and trying to use it may crash your program; it also prevents the Lua-side object from being released.
But trying to use nil instead of a released object may also cause you to crash, so in that sense it makes no difference. The Lua-side object not getting garbage collected is true unless the variable is local.

Re: removing images

Posted: Tue Oct 26, 2021 10:23 am
by pgimeno
ReFreezed wrote: Mon Oct 25, 2021 9:04 pm But trying to use nil instead of a released object may also cause you to crash, so in that sense it makes no difference. The Lua-side object not getting garbage collected is true unless the variable is local.
Yes, but a released object evaluates to true, while nil evaluates to false, so if you're checking using if myobject then... it will crash if you don't set it to nil (or false, which may be preferable in some cases, especially in arrays, to avoid creating a hole).

Re: removing images

Posted: Tue Oct 26, 2021 10:38 pm
by ReFreezed
pgimeno wrote: Tue Oct 26, 2021 10:23 am Yes, but a released object evaluates to true, while nil evaluates to false, so if you're checking using if myobject then... it will crash if you don't set it to nil
Sure, but then I'd say the variable should be set to nil not because "it's best to do so", but rather "because the other piece of code expects the variable to be an object or nil". If a variable is set to nil for no concrete reason then I would raise an eyebrow when looking at the code. Also, if an object is released and then used then the program is simply broken and should be fixed (e.g. by setting myobject to nil and adding 'if myobject then' where it matters).