While playing around with a game, I found that the more I was playing, the more hiccups came up. I thought about a leak, so I searched throughout the code and that the problem was in a pair of function, one that creates an object, one that destroy an object after it reaches a certain point and call the first function again, in a loop.
To be sure nothing else was causing the leak, I just wrote some code from scratch, where some objects are created and the destroyed. I didn’t use an enterFrame function for every object, to avoid any listener mistake or things like that.
local physics = require "physics";
physics.start();
physics.setGravity(0,0);
local layerObject = display.newGroup();
local function createObject()
local object = display.newImageRect(layerAsteroid, "object.png", 64,64);
--physics.addBody(object, {density = 5, friction = 2, bounce = 3});
object.x = 540
object.y = 160
--object = nil
end
local function enterFrame()
local num = layerObject.numChildren;
for i=num, 1, -1 do
local self = layerOBject[i];
self.x = self.x-10;
if self.done == nil and self.x \<= 420 then
self.done = 1;
createObject();
return true;
elseif self.x \<= -50 then
self.parent:remove(self)
self = nil;
return true;
end
end
end
Runtime:addEventListener("enterFrame", enterFrame);
local function garbagePrinting()
print(collectgarbage("count"));
print(system.getInfo("textureMemoryUsed"));
end
Runtime:addEventListener("enterFrame", garbagePrinting);
collectgarbage("setpause", 50); --this is so that the gc works fast and
--heavy, letting me see faster if it's leaking or not
createObject()
Like it is now, it shows no sign of leaks. The texture memory stay the same, so does the garbage count (after it starts its work). However, if you take out the comment in the line physics.addBody, you’ll see where the leak is. The texture memory stay the same, while the garbage count keeps increasing.
Am I doing something wrong? Is there a way to remove the body that I’m not seeing?
I already mailed Carlos about this, but just wanted to see if anybody has a solution for this Pretty bugging me. [import]uid: 8486 topic_id: 4715 reply_id: 304715[/import]