Hello guys,
I am starting my first game with gideros and have issue one problem so far.
It is about memory managment of inheritance class of Sprite object
It is not been cleaned when the objects are removed and i do not understand what the cause is!
My code:
[CODE]
EnemyObj = Core.class(Sprite);
function EnemyObj:init()
--self.speed = 1;
self:addEventListener(Event.ENTER_FRAME, self.update, self)
end
function EnemyObj:spawn()
self:setPosition(_W+_trX, RAND(0-_trY, _H+_trY))
end
function EnemyObj:destroy()
self:removeFromParent();
self = nil
end
function EnemyObj:update(event)
local x = self:getX();
self:setX(x - 5);
if (x < -200) then
self:spawn();
self:destroy();
end
end
Rocket = Core.class(EnemyObj);
function Rocket:init()
local texture = Texture.new("images/icons/rocket.png",true);
local bitmap = Bitmap.new(texture);
--self.speed = 5;
bitmap:setAnchorPoint(0.5,0.5);
bitmap:setRotation(180);
self:addChild(bitmap);
self:spawn();
end
Pillar = Core.class(EnemyObj);
function Pillar:init()
end
[/CODE]
and on touch i call: stage:addChild(Rocket.new())
But when the destroy function is called, the memory is not been cleaned. The object is removed from stage but still exists somehow?
Does anyone have an idea what could be wrong?
Comments
- You're not manually collecting garbage
- Memory usage is below amount required to trigger automatic collecting
then you won't see a reduction in memory usage.
Could someone explain?
Also please explain when the garbagecollector cleans automatically?
you may try to force it by calling collectgarbage() multiple times in a row, still it probably won't happen immediately.
So it is ok to have something in the memory while there is enough memory.
And here is a way to detect when your instance is being collected:
http://giderosmobile.com/forum/discussion/comment/12334#Comment_12334
So if most of the instance get collected and some of them are left, it is ok. They will be collected eventually.
But when you create lots of instances and none of them getting collected, then you have a problem (a reference that keeps them)
And actually the fact that 1 instance does not get collected could tell also that you are keeping an external reference to it (like a property of the scene or global variable).
The problem is when for example i create 500 Rocket object at the same time and after some time the destroy method&collectgarbage is called on all those 500 object, there is still one left somehow and will eventually trigger ONE enterframe event.
My conclusion is that there always 1 instance of rocket left somehow?
Are you sure you are not like making global variable accidentally instead of local, as:
--local rocket
for i=1, 500 do
self:addChild(Rocket.new()); -- self is a scene sprite
end
whenever all the objects are removed, one enterframe event still exists....
Maybe it is duo the inheritance gideros creates? So it always create one object of "EnemyObj"
It is always suggested to remove enterframe events, as they usually can be most consuming ones, remove them as soon as you don't need them.
Other approach would be creating one single enter frame loop, and creating Rocket:step method to move it and call for each rocket on each enter frame. This way you have also an advantage of pausing the game, by simply putting single check in enter frame in single event.
It is hard to pause if you do not cache those objects then hehehe