Lua class and destroy object in the clas

module (..., package.seeall);  
  
function new()  
 ---CONSTRUCTOR---------------------------------------  
 local poo = display.newImage("media/poo.png");  
 local myrand = math.random  
 poo.x = myrand(50, 400); poo.y = myrand(50,300);  
  
  
 -----------------------------------------------------   
 function remove\_monster( object )  
 if object ~= nil then  
 print("remove\_monst");  
 display.remove(object);  
 object = nil;  
 end  
 end  
  
 function poo:destroy()  
 display.remove(self)  
 self = nil  
 end  
  
  
 transition.to( poo, { time=1000, y=500, alpha=0, onComplete=remove\_monster } )  
 -----------------------------------------------------  
 -- Listener to Touch Screen   
 -----------------------------------------------------  
 function touch(e)  
 if (e.phase == "ended") then  
 local splash = display.newImage("media/poo\_splash".. math.random(1,5) ..".png");  
 transition.to( splash, { time=100, alpha=0, onComplete=destroy} )  
 --transition.to( splash, { time=100, alpha=0, onComplete=function() remove\_monster(poo); end;} )  
  
  
 end  
 return true;  
 end  
  
 -----------------------------------------------------  
 -- Listener  
 -----------------------------------------------------  
 poo:addEventListener( "touch", touch );  
 return poo;  
end  
  

Hi everybody,
i have the problem to destroy the object in the same class.

For example: when the transition complete, remove the object from the display and from the memory.

If i use the function destroy (OUTSIDE THE MODULE) there is no problem. But inside the module don’t work… don’t delete the memory!
Can somebody help me?!?! I need to free the memory calling a function INSIDE the module class.
Thank you so much.

[import]uid: 153233 topic_id: 27439 reply_id: 327439[/import]

Sounds like this should work, but you would need to test after using garbage collection. Simply setting nil wouldn’t free up the memory… [import]uid: 41884 topic_id: 27439 reply_id: 111557[/import]

I resolved in this way:

 function destroy(obj)  
 obj:removeSelf();  
 obj = nil;  
 end  
  
 function destroy\_music(obj)  
 audio.dispose( obj );  
 obj = nil;  
 end  

and

transition.to( e.target, { time=50, alpha=0, onComplete = function() destroy(e.target); destroy\_music(squish\_sound); end; } )  

Seems to work…:smiley: [import]uid: 153233 topic_id: 27439 reply_id: 112004[/import]