Hi
I'm facing an issue when using the sprite:moveWithCollisions
method. It looks like sprites that are removed after colliding with another sprite are not correctly freed from memory.
I'm using the method pretty much the same as described in the docs and the 2020 example:
local actualX, actualY, collisions, length = self:moveWithCollisions(self.x + dx, self.y + dy)
for i = 1, length do
local other = collisions[i].other
if self:alphaCollision(other) then
if other.isEnemy then
self:remove()
other:remove()
score += 1
end
end
end
The first few sprites are cleaned up correctly, but after a while, the memory footprint keeps increasing. I managed to update the 2020 example to show the issue more clearly, I'll attach it here:
Archive.zip (89.0 KB)
(Basically press A to toggle shooting, let it sit for ~10 minutes, and you'll see the memory ramp up.)
Initially I figured the GC is just not keeping up or waiting for a better time to clear the sprites, but even when you stop spawning sprites, and manually call collectgarbage()
, no memory is freed. Even stranger, this problem does not seem to occur on the simulator.
I also tried to just add/remove a bunch of sprites without doing any collision checking, and the memory behaves as it should, so I'm pretty sure there's something going on with the collision info items.