why does purge or remove not prevent memory leak when called from exitScene? (but does from calling scene - code attached)

Curious to know why a Purge or Remove does not prevent memory leaks when repeatedly entering a scene when called from the “exitScene” of the scene, BUT if I call the purge or remove from the calling scene it does stop memory leaks.

The memory leak I put in was the external “Level:new()” variable I pass to the scene. So I’m experimenting here re how to “reset” clear the scene of all variables including this one passed in (so was trying the purge or remove as a way to clear memory rather than doing it manually)

Code:

  
-- =============selection\_scene.lua=========================  
local scene = Storyboard.newScene()   
  
function scene:createScene( event )  
end  
  
function scene:enterScene( event )  
 local nextLevel = 1  
 local Level = require("level\_" .. 1)  
 local myLevel = Level:new()  
  
 -- \*\*\*\* EITHER OF THE TWO LINES BELOW ENSURES A MEMORY LEAK DOESN'T OCCUR   
 -- Storyboard.purgeScene( "game\_scene" )  
 -- Storyboard.removeScene( "game\_scene" )  
  
 local options =  
 {  
 params = { level = myLevel }  
 }  
 Storyboard.gotoScene( "game\_scene", options )  
end  
  
function scene:exitScene( event )  
end  
  
function scene:destroyScene( event )  
end  
  
-- Scene Listeners  
scene:addEventListener( "createScene", scene )  
scene:addEventListener( "enterScene", scene )  
scene:addEventListener( "exitScene", scene )  
scene:addEventListener( "destroyScene", scene )  
  
return scene  
  
-- ================gamescene.lua============================  
local scene = Storyboard.newScene()   
local widget = require "widget"  
  
local onButtonEvent = function (event )  
 if event.phase == "release" then  
 print( "About to go back to Selection Scene" )  
 Storyboard.gotoScene( "selection\_scene")  
 end  
end  
  
function scene:createScene( event )  
  
 local myButton = widget.newButton{  
 id = "btn001",  
 left = 100,  
 top = 200,  
 label = "Widget Button",  
 width = 150, height = 28,  
 cornerRadius = 8,  
 onEvent = onButtonEvent  
 }  
 scene.view:insert( myButton ) -- if using older build, use: myButton.view instead  
end  
  
function scene:enterScene( event )  
 scene.view:insert( event.params.level )  
  
 collectgarbage()  
 print( "MemUsage: " .. collectgarbage("count") .. " KB" )  
 print( "textureMemoryUsed: " .. (system.getInfo( "textureMemoryUsed" )/1000000) .. " MB" )  
end  
  
function scene:exitScene( event )  
  
 -- ISSUE: \*\* These do NOT prevent a memory leak here (but do when run selection\_scene.lua \*\*  
  
 -- Storyboard.purgeScene( "game\_scene" )  
 -- Storyboard.removeScene( "game\_scene" )  
end  
  
function scene:destroyScene( event )  
  
end  
  
-- Scene Listeners  
scene:addEventListener( "createScene", scene )  
scene:addEventListener( "enterScene", scene )  
scene:addEventListener( "exitScene", scene )  
scene:addEventListener( "destroyScene", scene )  
  
return scene  
  
-- ===================level\_1.lua=======================  
  
Level = {}  
function Level:new()  
 print ("Level:new()")  
 local bgGroup = display.newGroup()  
  
 -- Variable  
 bgGroup.myValue = "Initial Setting"  
  
 -- Display Object  
 bgGroup.myCircle = display.newCircle( 100, 100, 20 )  
 bgGroup.myCircle:setFillColor(128,128,128)  
 bgGroup:insert(bgGroup.myCircle)  
  
 return bgGroup  
  
end  
return Level  
  
-- =========================================================  
  

[import]uid: 140210 topic_id: 34525 reply_id: 334525[/import]

I would try calling the purge/remove in didExitScene.(http://docs.coronalabs.com/api/event/scene/didExitScene/index.html)

Could also be that garbage collection isn’t keeping up with the scene transitions, so you could try calling timer.performWithDelay(1, function() collectgarbage(“collect”) end). right after you do the purges.

This is just conjecture, I don’t actually know what the problem is but I hope it helps get you close to solving your problem.

-Treb [import]uid: 181948 topic_id: 34525 reply_id: 137304[/import]

ok thanks for the idea (I was just curious) [import]uid: 140210 topic_id: 34525 reply_id: 137306[/import]

I would try calling the purge/remove in didExitScene.(http://docs.coronalabs.com/api/event/scene/didExitScene/index.html)

Could also be that garbage collection isn’t keeping up with the scene transitions, so you could try calling timer.performWithDelay(1, function() collectgarbage(“collect”) end). right after you do the purges.

This is just conjecture, I don’t actually know what the problem is but I hope it helps get you close to solving your problem.

-Treb [import]uid: 181948 topic_id: 34525 reply_id: 137304[/import]

ok thanks for the idea (I was just curious) [import]uid: 140210 topic_id: 34525 reply_id: 137306[/import]