removeSelf() & nil mystery to be solved (yet again)

It appears that what I’m about to post has posed a problem to many (for example, a variation of it appears here:http://developer.anscamobile.com/forum/2010/09/06/problem-removeself-when-adding-bodys-table). Here’s the code:

--import the ; class for sharper text  
local retina = require("retina");  
  
--Create table to hold missiles  
local silo = {}  
  
--Index for the silo table  
local ndx = 0;  
  
--spawn missiles on command  
function spawnMissiles()  
 ndx = ndx + 1;  
 --Create a single instance of a missile (to be assigned to multiple table entries later  
 --This is a custom method that loads newImageRect's)  
 silo[ndx] = retina.newImage({image="images/missile.png",width=70,height=36,origin="center-right"});  
 silo[ndx].x = -80; silo[ndx].y = display.contentHeight/2;  
 transition.to(silo[ndx], {time=1500, x=500, onComplete=removeMissiles});  
end  
  
--remove missiles if they cross the left-half of the screen  
function removeMissiles()  
 for i = 1, #silo do  
 local s = silo[i]  
 if (s.x) then  
 print(s.x)  
 if (s.x \> \_W/2) then  
 s:removeSelf();  
 end   
 end  
 end  
end  
  
--spawn 10 missiles  
spwnr = timer.performWithDelay(500, spawnMissiles, 10);  

and here’s the error (the stated line number and line number show above match):

Runtime error ...ts/iPhoneDev/Corona Projects/MissilePhysics/main.lua:26: attempt to perform arithmetic on global '\_W' (a nil value) stack traceback: [C]: ? ...ts/iPhoneDev/Corona Projects/MissilePhysics/main.lua:26: in function <...ts projects><br> (tail call): ?<br> ?: in function <?:866><br> ?: in function <?:214><br>

My guess is that the error has something to do with trying to get rid of the object in the silo table before transition.to is done with it. But, that’s just a guess. The folks on the board are quite generous with their time and I certainly do appreciate any responses that help shed light on what in the logic is causing this foul up. [import]uid: 13345 topic_id: 4753 reply_id: 304753[/import] </…ts>

My bad: I mean to replace the _W constant with display.contentWidth – here’s the correct faulty code:

--import the ; class for sharper text  
local retina = require("retina");  
  
--Create table to hold missiles  
local silo = {}  
  
--Index for the silo table  
local ndx = 0;  
  
--spawn missiles on command  
function spawnMissiles()  
 ndx = ndx + 1;  
 --Create a single instance of a missile (to be assigned to multiple table entries later  
 --This is a custom method that loads newImageRect's)  
 silo[ndx] = retina.newImage({image="images/missile.png",width=70,height=36,origin="center-right"});  
 silo[ndx].x = -80; silo[ndx].y = display.contentHeight/2;  
 transition.to(silo[ndx], {time=1500, x=500, onComplete=removeMissiles()});  
end  
  
--remove missiles if they cross the left-half of the screen  
function removeMissiles(obj)  
 for i = 1, #silo do  
 local s = silo[i]  
 if (s.x) then  
 print(s.x)  
 if (s.x \> display.contentHeight/2) then  
 s:removeSelf();  
 end   
 end  
 end  
end  
  
--spawn 10 missiles  
spwnr = timer.performWithDelay(500, spawnMissiles, 10);  

and the error

Runtime error ...ts/iPhoneDev/Corona Projects/MissilePhysics/main.lua:27: attempt to call method 'removeSelf' (a nil value) stack traceback: [C]: in function 'removeSelf' ...ts/iPhoneDev/Corona Projects/MissilePhysics/main.lua:27: in function 'removeMissiles' ...ts/iPhoneDev/Corona Projects/MissilePhysics/main.lua:17: in function '\_listener' ?: in function <?:441> ?: in function <?:214> [import]uid: 13345 topic_id: 4753 reply_id: 15189[/import]

oh!

I got the same error too !

removeSelf() seems only work on display items,fail with table objects.

but how can I clean up memory ?

anyone got ideas ?

thanks. [import]uid: 21680 topic_id: 4753 reply_id: 34543[/import]

removeSelf is a function added by Corona to display objects tables. This is not a standard lua function. To delete a regular table, just nil it and it will be GCed. [import]uid: 51516 topic_id: 4753 reply_id: 34548[/import]