I was having issue with the same thing. I took the code example here provided by Walter which does just what it says it does.
BUT, I modified it a little to stream it down and to fit the issue I am having, and discovered a slight issue with it.
When assigning a touch listener to the ‘image object’ created from an imagesheet the texture memory does not get released!!!
When the listener touch event is triggered in my sample code here, in the function I remove the listener, remove the image object correctly and ‘nil’ the image object. And then ‘nil’ the imagesheet … MEMORY CONSUMED BY THE IMAGE SHEET DOES NOT CLEAR.
HOWEVER, if I run the same exact code, but make the touch listener on Runtime, and have that touch event remove the image object, and the all the same object clean up stuff I do when it is attached to the image object, it DOES indeed clear the texture memory.
The issue is this: when an event listener is attached to a display object that was created from an imagesheet, even after proper steps to remove and nil that object, the imagesheet when nil’d does not release the texture memory!
The question is: is there another step to follow in cases where the image object(created from an imagesheet) has an event listener attached ???
OR is this a bug with the imagesheet?
I have both examples of the code I tested here :
- with event listener on the image object (does not release memory)
local options = { width = 100, height = 100, numFrames = 9, sheetContentWidth = 300, sheetContentHeight = 300}
local sheet = graphics.newImageSheet( “numSheet300.png”, options )
local s = display.newImage( sheet, 1 )
s.x = 100
s.y = 100
print( system.getInfo( “textureMemoryUsed” ) )
local function clearIt(e)
if e.phase == “ended” then
s:removeEventListener(“touch”, clearIt)
s:removeSelf()
s = nil
collectgarbage( “collect” )
print( system.getInfo( “textureMemoryUsed” ) ) --> 1048576
sheet = nil
collectgarbage( “collect” ) --> STILL 1048576
print( system.getInfo( “textureMemoryUsed” ) ) --> 1048576
end
end
s:addEventListener(“touch”, clearIt)
- event listener on Runtime (does release the memory)
local options = { width = 100, height = 100, numFrames = 9, sheetContentWidth = 300, sheetContentHeight = 300}
local sheet = graphics.newImageSheet( “numSheet300.png”, options )
local s = display.newImage( sheet, 1 )
s.x = 100
s.y = 100
print( system.getInfo( “textureMemoryUsed” ) )
local function clearIt(e)
if e.phase == “ended” then
Runtime:removeEventListener(“touch”, clearIt)
s:removeSelf()
s = nil
collectgarbage( “collect” )
print( system.getInfo( “textureMemoryUsed” ) ) --> 1048576
sheet = nil
collectgarbage( “collect” ) --> CLEARS THE MEMORY
print( system.getInfo( “textureMemoryUsed” ) ) --> 0
end
end
Runtime:addEventListener(“touch”, clearIt)
I really like the ImageSheet … it is a great tool and does some very good things, and I have it at the core of what my app is doing, so I need to find a way to fix this memory leak.
Thanks!
[import]uid: 148857 topic_id: 27837 reply_id: 140858[/import]