Removing in turning to nil an object using a function.

Good day

I was wondering how am I gonna turn my Object into nil, using this code, it doesn’t seem to turn into a nil:

local function remover(self) self:removeSelf() -- Removes the object self = nil -- It turns nil here end local rect = display.newRect(10,10,10,10) transition.to(rect, {x = 200, time = 2000, onComplete = remover) -- Moves the rect 200px and remove it after transition local function rectChecker(event) print(rect) -- Even if it is removed it always returns a "table:x012301blabla" end Runtime:addEventListener("enterFrame", rectChecker)

What is the proper way in removing and turning it into nil using a single function?

I guess that’s because you are checking “rect” object in every frame so the object reference couldn’t be removed. Can you try this?

local function remover(rect) print (rect) display.remove(rect) rect = nil print (rect) end local rect = display.newRect(10,10,10,10) print (rect) transition.to(rect, {x = 200, time = 2000, onComplete = function () remover(rect) end})

You did remove the display object and free up the memory, you just need to nil out the “rect” handle directly.  If you really want to double check that the rect display object was removed, or indeed verify it still exists, you can check for the removeSelf() method on the object.  So:

local function rectChecker(event) print(rect) -- Even if it is removed it always returns a "table:x012301blabla" --If rect exists in memory (is not nil) but has no removeSelf method then it was removed, nil out the handle if rect and not rect.removeSelf then rect = nil end end Runtime:addEventListener("enterFrame", rectChecker)

Both of your suggest code works, phil_s, as long as it frees up memory, and everything is okay, I’m just worried that my memory is being loaded because my objects are not properly removed.

If you want to be sure the memory is freed, you can use this code to see it: https://gist.github.com/JesterXL/5615023

https://github.com/pancinteractive/performancemeter/blob/master/performance.lua

You can track both your texture memory and Lua memory with this library.

Also, you should insert all your display objects to display groups and insert those display groups into the main group so Corona can handle all of them for you when you are changing scenes in your game. Here:

https://docs.coronalabs.com/guide/media/displayObjects/index.html#removing-display-objects

I guess that’s because you are checking “rect” object in every frame so the object reference couldn’t be removed. Can you try this?

local function remover(rect) print (rect) display.remove(rect) rect = nil print (rect) end local rect = display.newRect(10,10,10,10) print (rect) transition.to(rect, {x = 200, time = 2000, onComplete = function () remover(rect) end})

You did remove the display object and free up the memory, you just need to nil out the “rect” handle directly.  If you really want to double check that the rect display object was removed, or indeed verify it still exists, you can check for the removeSelf() method on the object.  So:

local function rectChecker(event) print(rect) -- Even if it is removed it always returns a "table:x012301blabla" --If rect exists in memory (is not nil) but has no removeSelf method then it was removed, nil out the handle if rect and not rect.removeSelf then rect = nil end end Runtime:addEventListener("enterFrame", rectChecker)

Both of your suggest code works, phil_s, as long as it frees up memory, and everything is okay, I’m just worried that my memory is being loaded because my objects are not properly removed.

If you want to be sure the memory is freed, you can use this code to see it: https://gist.github.com/JesterXL/5615023

https://github.com/pancinteractive/performancemeter/blob/master/performance.lua

You can track both your texture memory and Lua memory with this library.

Also, you should insert all your display objects to display groups and insert those display groups into the main group so Corona can handle all of them for you when you are changing scenes in your game. Here:

https://docs.coronalabs.com/guide/media/displayObjects/index.html#removing-display-objects