This had me stumped for a while, as I thought one of my modules was leaking memory. Maybe I’m omitting something, but the docs all seem to say that DisplayObjects with RigidBodies attached are disposed of by the usual removeSelf(), or spriteSheet:dispose(). But this doesn’t seem to work when a physics body is attached to a sprite. Regular DisplayObjects with physics bodies behave as expected. Here’s a minimalist example:
require "sprite"
require "out"
require "physics"
physics.start()
local prevTime = system.getTimer()
local ix = 1
local data, spriteSheet, spriteSet
local instances = {}
local onFrame
local startup
local tm = function() print("Texture memory used: " .. system.getInfo("textureMemoryUsed")) end
local f1 = function()
data = out.getSpriteSheetData()
spriteSheet = sprite.newSpriteSheetFromData("out.png", data)
spriteSet = sprite.newSpriteSet(spriteSheet, 1, 64)
tm()
end
local f2 = function()
for i = 1, 10 do
local instance = sprite.newSprite(spriteSet)
physics.addBody(instance, { density = 1.0, friction = 0.3, bounce = 0.2, radius = 50} )
instances[i] = instance
end
end
local f3 = function()
-- this doesn't free memory if physics body is attached
for i = 1, #instances do
instances[i]:removeSelf()
end
-- neither does this
spriteSheet:dispose()
end
local f4 = function()
Runtime:removeEventListener("enterFrame", onFrame)
tm()
end
local tab = { f1, f2, f3, f4 }
onFrame = function(event)
if event.time - prevTime \>= 2000 then
prevTime = event.time
tab[ix]()
ix = ix + 1
end
end
tm()
startup = function(event)
if event.phase == "began" then
print("startup: adding onFrame")
Runtime:addEventListener("enterFrame", onFrame)
ix = 1
end
end
display.getCurrentStage():addEventListener("touch", startup)
If you comment out “physics.addBody” all works fine and the final check shows zero. Otherwise there’s no reduction. In my case:
Texture memory used: 0
startup: adding onFrame
Texture memory used: 2097152
Texture memory used: 2097152
After commenting out addBody call:
Texture memory used: 0
startup: adding onFrame
Texture memory used: 2097152
Texture memory used: 0
If you iterated through a few times you’ll find that no additional memory is allocated. It’s just that the first allocation stays. Not so good if loading multiple sequential levels.
For any new users wondering why the convoluted test routine, texture memory is not freed - or at least up to date reporting of it - doesn’t happen until the next enterFrame event.
I can provide the “out” files from TexturePacker but I don’t know how to do forum message attachments. But it’s pretty easy to test. Or preferably I can be corrected by one of the fine Corona forum identities.
[import]uid: 3953 topic_id: 2665 reply_id: 302665[/import]