a memory leak question about my storyboard usage

I found I have a memory leak, and I have a question when using storyboard, please help.

For example, I have a storyboard and I create some display object in createScene

local savedObjs = {} function scene:createScene( event ) local group = self.view for i=1,10 do local obj = display.newImage(...) savedObjs[i] = obj group:insert(obj) end end function scene:exitScene(event) storyboard.purgeScene("... current scene...") end function scene:destroyScene(event) for i=1,10 do savedObjs[i] = nil end savedObjs = nil end

If I exit and enter this scene repeatedly, the memory grows. 

I am trying to figure out where the memory leak is. 

Hi @joe528,

I see where you’re creating the images, but I don’t see a line where you’re removing them from the display. Try this loop in your destroyScene event:

[lua]

for i=1,10 do

   display.remove( savedObjs[i] )

   savedObjs[i] = nil

end

[/lua]

Best regards,

Brent

Adding on to what Brent is saying.  Since you are running that loop in your destoryScene(), you’re just erasing the pointers to your display objects.  You do have to remove them before you nil them.

However if you insert your objects in createScene() into the scene’s view (“group” or “screenGroup”) then you don’t need to manually remove them.  Storyboard will clean up objects it manages for you.

@Rob, like you said, the storyboard will clean up the objects. So I don’t need to remove them again right?

Do I need to nil them like I did? Or I can actually do nothing at all in the destroyScene() function?

Correct.  You do not need to remove them or nil them… if you’ve inserted them into the scene’s group.  Display objects you don’t put in there, and other things that are not display objects (timers, audio, etc. ) are still your responsibility.

@Rob It’s what I did by inserting display objects into the scene’s group. Originally it was ok, but recently I found if I don’t manually remove them, my texture memory increases once I exit and re-enter the scene.

It sounds weird and I feel it’s weird too.

For example, if I don’t insert display objects into the scene group directly, but kind of “indirectly” like this

local page = {} local function abc(num)    page[num] = display.newGroup()  -- assume page[num] is nil    scene.view:insert(page[num])    local obj = display.newImage(...)    obj:insert(page[num]) end

Does it make difference? 

I am pretty sure all my display groups are in the storyboard group because when the scene exit, they are gone too. But it’s kind of weird to me that I need to manually remove them now in destroy function or the texture memory increases.

It doesn’t hurt to remove it yourself, but if you do you have to call each object’s :removeSelf() or call display.remove(obj) before you nil it.

In my original code for this question, I didn’t remove by myself but I did nil them, is it ok? Will the display objects be removed by storyboard anyway?

Storyboard should remove them.  If you just nil them, without removing them, you’ve killed the reference to them so they can’t be removed and you will leak.  So either you remove and nil or do neither.

Hi @joe528,

I see where you’re creating the images, but I don’t see a line where you’re removing them from the display. Try this loop in your destroyScene event:

[lua]

for i=1,10 do

   display.remove( savedObjs[i] )

   savedObjs[i] = nil

end

[/lua]

Best regards,

Brent

Adding on to what Brent is saying.  Since you are running that loop in your destoryScene(), you’re just erasing the pointers to your display objects.  You do have to remove them before you nil them.

However if you insert your objects in createScene() into the scene’s view (“group” or “screenGroup”) then you don’t need to manually remove them.  Storyboard will clean up objects it manages for you.

@Rob, like you said, the storyboard will clean up the objects. So I don’t need to remove them again right?

Do I need to nil them like I did? Or I can actually do nothing at all in the destroyScene() function?

Correct.  You do not need to remove them or nil them… if you’ve inserted them into the scene’s group.  Display objects you don’t put in there, and other things that are not display objects (timers, audio, etc. ) are still your responsibility.

@Rob It’s what I did by inserting display objects into the scene’s group. Originally it was ok, but recently I found if I don’t manually remove them, my texture memory increases once I exit and re-enter the scene.

It sounds weird and I feel it’s weird too.

For example, if I don’t insert display objects into the scene group directly, but kind of “indirectly” like this

local page = {} local function abc(num)    page[num] = display.newGroup()  -- assume page[num] is nil    scene.view:insert(page[num])    local obj = display.newImage(...)    obj:insert(page[num]) end

Does it make difference? 

I am pretty sure all my display groups are in the storyboard group because when the scene exit, they are gone too. But it’s kind of weird to me that I need to manually remove them now in destroy function or the texture memory increases.

It doesn’t hurt to remove it yourself, but if you do you have to call each object’s :removeSelf() or call display.remove(obj) before you nil it.

In my original code for this question, I didn’t remove by myself but I did nil them, is it ok? Will the display objects be removed by storyboard anyway?

Storyboard should remove them.  If you just nil them, without removing them, you’ve killed the reference to them so they can’t be removed and you will leak.  So either you remove and nil or do neither.