Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
How should a Button be garbage collected? — Gideros Forum

How should a Button be garbage collected?

lvkdevlvkdev Member
edited August 2013 in Bugs and issues
I am not able to unload Buttons to free up memory - it does not seem to work.

Using the following code:
 
print("TextureMemory at startup", application:getTextureMemoryUsage())
 
local buttonPlay = Button.new(Bitmap.new(Texture.new("btnNormal.png", true)), Bitmap.new(Texture.new("btnPressed.png", true)))
buttonPlay:setPosition(100, 100)
 
print("TextureMemory after loading buttonPlay", application:getTextureMemoryUsage())
 
local function playClicked()
	print("play clicked")
	playTween = GTween.new(buttonPlay, 1, {alpha=0}, {delay=0, easing=easing.linear, dispatchEvents=true})
	playTween:addEventListener("complete", function()
		playTween = nil
		unloadButton()
		print("TextureMemory after unloading button on Tween complete", application:getTextureMemoryUsage())
	end)
end
 
buttonPlay:addEventListener("click", playClicked)
stage:addChild(buttonPlay)
 
function onMouseDown()
	unloadButton()
	print("TextureMemory after unloading button onMouseDown", application:getTextureMemoryUsage())
end
stage:addEventListener(Event.MOUSE_DOWN, onMouseDown)
 
function unloadButton(msg)
	buttonPlay:removeEventListener("click", playClicked)
	stage:removeChild(buttonPlay)
	buttonPlay = nil
 
	collectgarbage("collect")
	collectgarbage("collect")
	collectgarbage("collect")
end
I get the following output:

Scenario 1: Click on the button, to invoke the GTween
TextureMemory at startup 0
TextureMemory after loading buttonPlay 128
play clicked
TextureMemory after unloading button on Tween complete 128

Scenario 2: Touch the screen to unload button without invoking GTween
TextureMemory at startup 0
TextureMemory after loading buttonPlay 128
TextureMemory after unloading button onMouseDown 64

What needs to be done in addition to removing the button from the stage and setting it to nil?

Comments

  • Well garbage is not collected immediately especially the texture, because it is saved in the memory in case it gets reused for some time.

    But all in all it seems you are doing all right.

    What you can do, is to attach this kind of witchery to your Button (or any other class/object) class:
    function MyClass:init()
       self.proxy = newproxy(true)
       getmetatable(self.proxy).__gc = function() print("collected!") end
    end
    :
    foo=MyClass.new()
    so it will print "collected!" inside output window when the object gets collected, but as I said with texture it can be even longer.

    Likes: SinisterSoft

    +1 -1 (+1 / -0 )Share on Facebook
  • @ar2rsawseen - Ok. Thanks.
    But, any clues as to why there would be a difference between Scenario 1 and scenario 2 above? Without tween, the memory comes down from 128 to 64 after unloading the button. But with tween, there is no reduction. Seems like setting the tween object to nil is not enough and it is somehow holding on to the target button?
Sign In or Register to comment.