Hello @DPN Sweden and @Haroon,
Unfortunately, I don’t have a better “guide” than the one you linked to read about proper cleanup. It’s just a matter of checking everything on the “most common list” and ensuring that you cleared and nil’led it out of Lua’s memory. This list is what I posted above (and there might be even more to add), but I always check for these:
-
object listeners (tap, touch, etc.) / Runtime listeners – basically, anywhere you type :addEventListener()
, you need to ensure that you :removeEventListener()
on the same object and listening function when you exit the scene.
-
incomplete timers – if you have a timer running for more than a few milliseconds, I suggest you attach it to a local reference, then when you exit the scene, you cancel that timer explicitly.
-
unfinished transitions – same approach as timers
-
tables containing references to other objects – sometimes, you might add a reference to an object (like a button) to another Lua table, so you have those buttons as a known collection. When you exit the scene, ensure that you FIRST empty out the containing table", THEN you remove the items themselves.
-
backwards-iterate through tables to clear items – if you are looping through a table of objects to clear them, never loop from 1 to the # of table items. Instead, loop from the # of items backwards to 1. Why? Because if you start at #1, and clear that object, the #2 entry then becomes entry #1, but Lua has skipped to #2, which was previously #3. In other words, you will only clear every other item in the table. Working from the total number backward to 1, you clear out all of them.
--example table
local myButtons = { button1, button2, button3, button4 }
--BAD!
for i=1,#myButtons do
myButtons[i]:removeEventListener( "tap", onButtonTap )
display.remove( myButtons[i] ) ; myButtons[i] = nil
end
--GOOD!
for i=#myButtons,1,-1 do --loop backwards from end to start! (-1 specifies reverse iteration)
myButtons[i]:removeEventListener( "tap", onButtonTap )
display.remove( myButtons[i] ) ; myButtons[i] = nil
end
-
streaming audio or audio channels not disposed – you MUST dispose audio when you’re done with it. On scene exit, you should STOP all audio channels, then dispose of all channels too.
--load the sound
local mySound = audio.loadSound( "audio/harp.mp3" )
--on scene exit, stop all audio and dispose the sound
audio.stop()
audio.dispose( mySound ) ; mySound = nil
Hope this helps! Memory leaks are the bane of all programmers, and proper cleanup is the only way to prevent those leaks. Storyboard certainly isn’t “leak-proof” either; I see plenty of forum posts about Storyboard modules leaking memory because the proper cleanup steps weren’t followed.
Brent [import]uid: 200026 topic_id: 33674 reply_id: 134046[/import]