Hey, so I’ve been combing through my code recently, trying to iron out some lingering memory leaks. I’ve noticed a peculiarity regarding display groups.
I made a test scenario like this:
local bigGroup = display.newGroup()
local subGroup1 = display.newGroup()
local subGroup2 = display.newGroup()
local subGroup3 = display.newGroup()
bigGroup:insert(subGroup1)
bigGroup:insert(subGroup2)
bigGroup:insert(subGroup3)
I then created some display objects and inserted them into the three sub-groups. Now here’s the interesting part… It has been my understanding that removing a group will remove itself and all it’s children (including other groups) so I tested it.
First scenario:
local function removeAll()
display.remove(bigGroup)
bigGroup = nil
end
timer.performWithDelay(6000, removeAll, 1)
Everything seemed to work. All my display objects went away and my memUsage after this removeAll() function was around 119kb. But I wasn’t confident this was the best way so I tried it again, this time removing the three sub-groups first.
Second scenario:
local function removeAll()
display.remove(subGroup1)
subGroup1 = nil
display.remove(subGroup2)
subGroup2 = nil
display.remove(subGroup3)
subGroup3 = nil
display.remove(bigGroup)
bigGroup = nil
end
timer.performWithDelay(6000, removeAll, 1)
So this should effectively do the exact same thing, right? Well, visually yeah, but not according to Lua memory. Method two resulted in a memUsage of 117kb afterwards, a better result than just removing the main group. Granted 2kb isn’t much, but this was a simple example and I fear for what could happen in a large project.
Maybe I’m misunderstanding something fundamental here, but this looks like a gigantic problem for anyone using Director / Storyboard and just assuming that everything in your localGroup is going to be whisked away for you (which seems to very much be the general consensus around the forums).
Has anyone else encountered anything like this or would care to comment?
Brandon [import]uid: 136211 topic_id: 31652 reply_id: 331652[/import]