removing images

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
User avatar
GVovkiv
Party member
Posts: 668
Joined: Fri Jan 15, 2021 7:29 am

Re: removing images

Post 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?
User avatar
pgimeno
Party member
Posts: 3544
Joined: Sun Oct 18, 2015 2:58 pm

Re: removing images

Post 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.
User avatar
GVovkiv
Party member
Posts: 668
Joined: Fri Jan 15, 2021 7:29 am

Re: removing images

Post 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
User avatar
ReFreezed
Party member
Posts: 612
Joined: Sun Oct 25, 2015 11:32 pm
Location: Sweden
Contact:

Re: removing images

Post 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.
Tools: Hot Particles, LuaPreprocess, InputField, (more) Games: Momento Temporis
"If each mistake being made is a new one, then progress is being made."
User avatar
pgimeno
Party member
Posts: 3544
Joined: Sun Oct 18, 2015 2:58 pm

Re: removing images

Post 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).
User avatar
ReFreezed
Party member
Posts: 612
Joined: Sun Oct 25, 2015 11:32 pm
Location: Sweden
Contact:

Re: removing images

Post 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).
Tools: Hot Particles, LuaPreprocess, InputField, (more) Games: Momento Temporis
"If each mistake being made is a new one, then progress is being made."
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 40 guests