Proper object destruction without memory leak?

I wonder how I can work with images when using functions or other kind of “pointers”.

Here is an example:

myObj=display.newImageRect("myObj.png",64,64) local doSomething = function (obj)     obj.x=obj.x+1 end doSomething (myObj)

How can I make sure this object is moved from memory 100%?

Is it enough to destroy the object “myObj” later here? Or do I have to think about “obj” inside the function also?

Thx for your help!

@c.noeth,
 
No.  You do not need to worry about the local variable in the function:

-- 1. Slight change. Store your object in a local, not a global -- local myObj = display.newImageRect("myObj.png",64,64) -- 2. obj in the function is a temporary/local variable. Once the -- function ends, it falls out of scope and is automatically discarded. -- (The variable, not the contents) -- local function doSomething( obj ) obj.x = obj.x + 1 end -- 3. This call passes a reference to 'myObj' into the function -- doSomething( myObj ) -- 4. After this code executes, the object will be ready for garbage collection. -- i.e. Some time later, LUA can reclaim the memory -- display.remove( myObj ) -- Tip: Use this instead of myObj:removeSelf(), it is safer. myObj = nil

Thank you for the fast and detailed answer!!! Helps a lot! :slight_smile:

one more question…

what about some table values, like this:

local myObj = display.newImageRect("myObj.png",64,64) myObj.trans1=function() transition.to(myObj,{time=300,x=200,onComplete=function() myObj.trans2( ) end}) end myObj.trans2=function() transition.to(myObj,{time=300,x=0,onComplete=function() myObj.trans1( ) end}) end myObj.clonobj=display.newImageRect("cloneobj.png",64,64)

is it enough to delete myObj then like for example

display.remove( myObj ) myObj = nil

or do I have to delete the table values first? What about the clone object?

Removing the object and nilling it will take care of the table values you are creating.

Rob

That was exactly what I have hoped to hear Rob! :slight_smile: Thank you!

c.noeath, you will get errors if you just delete objects that have transitions.

the safest approach is to first cancel the transitions then remove the objects, same applies to timers.

@c.noeth,
 
No.  You do not need to worry about the local variable in the function:

-- 1. Slight change. Store your object in a local, not a global -- local myObj = display.newImageRect("myObj.png",64,64) -- 2. obj in the function is a temporary/local variable. Once the -- function ends, it falls out of scope and is automatically discarded. -- (The variable, not the contents) -- local function doSomething( obj ) obj.x = obj.x + 1 end -- 3. This call passes a reference to 'myObj' into the function -- doSomething( myObj ) -- 4. After this code executes, the object will be ready for garbage collection. -- i.e. Some time later, LUA can reclaim the memory -- display.remove( myObj ) -- Tip: Use this instead of myObj:removeSelf(), it is safer. myObj = nil

Thank you for the fast and detailed answer!!! Helps a lot! :slight_smile:

one more question…

what about some table values, like this:

local myObj = display.newImageRect("myObj.png",64,64) myObj.trans1=function() transition.to(myObj,{time=300,x=200,onComplete=function() myObj.trans2( ) end}) end myObj.trans2=function() transition.to(myObj,{time=300,x=0,onComplete=function() myObj.trans1( ) end}) end myObj.clonobj=display.newImageRect("cloneobj.png",64,64)

is it enough to delete myObj then like for example

display.remove( myObj ) myObj = nil

or do I have to delete the table values first? What about the clone object?

Removing the object and nilling it will take care of the table values you are creating.

Rob

That was exactly what I have hoped to hear Rob! :slight_smile: Thank you!

c.noeath, you will get errors if you just delete objects that have transitions.

the safest approach is to first cancel the transitions then remove the objects, same applies to timers.