Hi! I’m completely new to programming, and I have some questions. I have read about display objects, and the “Cleaning Up Display Objects And Event Listeners” tutorial, but I feel like I’m missing something. Whenever I push a button I made, lua memory increases. It keeps increasing every time I push the button, and never decreases again. Here is my code:
[lua]
local function checkMemory()
collectgarbage( “collect” )
local memUsage_str = string.format( “MEMORY = %.3f KB”, collectgarbage( “count” ) )
print( memUsage_str, "TEXTURE = "…(system.getInfo(“textureMemoryUsed”) / (1024 * 1024) ) )
end
timer.performWithDelay( 1000, checkMemory, 0 )
local moveLeftButton
leftButtonPressed = false
function activateUI( event )
buttonGraphics()
moveLeftButton:addEventListener( “touch”, touchButtonLeft )
end
function buttonGraphics( event )
if leftButtonPressed == false then
moveLeftButton = display.newImage(“images/ButtonLeft.png”)
moveLeftButton.x = display.contentWidth / 2
moveLeftButton.y = display.contentHeight / 2
moveLeftButton.width = display.contentWidth / 5
moveLeftButton.height = moveLeftButton.width
else
moveLeftButton = display.newImage(“images/ButtonLeftPushed.png”)
moveLeftButton.x = display.contentWidth / 2
moveLeftButton.y = display.contentHeight / 2
moveLeftButton.width = display.contentWidth / 5
moveLeftButton.height = moveLeftButton.width
end
end
function touchButtonLeft( event )
if event.phase == “began” then
leftButtonPressed = true
buttonGraphics()
elseif event.phase == “ended” then
leftButtonPressed = false
buttonGraphics()
end
end
activateUI() – this is called from a level module
[/lua]
So, the idea is that I reserve the variable name, draw the graphic once, then check whether or not the graphic is being touch, and if that’s the case, I’m changing it until the button is released. It’s not the .png files being added again and again, as that would increase the texture memory usage. Only lua memory increases. Can anyone tell me what causes the memory leak on button touch? What’s the easiest way to avoid this?
Thanks a bunch!