composer.removeScene with newSnapshot = Memory Leak

I’ll see what I can find out.

Rob

Hi Rob. It’s been a while, did you find out anything?

Hi @kilopop,

This probably is the most obvious question ever, but I assume you also tested removal of the snapshot object itself (display.remove()) after you removed the objects that were contained within it? And doing so still resulted in a memory leak?

Brent

If you see the example above, the snapshot object is inserted into self.view and therefore should be destroyed/disposed by Composer, correct?

Hello.
I know that it’s quite old post but I’m also encountered this issue.
I measure memory using **collectgarbage(“count”) **and see that it only increases over time

Not sure if I need to create separate bug report for this

Hi Benzeliden. I can say Brent’s suggestion to remove the snapshot  after  you remove the objects that are contained within it does work. But this seems like a work-around given the snapshot is an object in another group that is itself disposed. Snapshots then do not behave like other display objects in this regard.

Well, removing snapshot doesn’t work. The only thing that works for me is to remove every object in snapshot.group manually

I prepare sample code:

local function createSnapshot(parent) local snapshot = display.newSnapshot(parent, 60 , 60) local rectLeft = display.newRect(5, 10, 10, 20) rectLeft:setFillColor( 1,0,0 ) snapshot.group:insert(rectLeft) local rectCenter = display.newRect(15, 10, 10, 20) rectCenter:setFillColor( 0,1,0 ) snapshot.group:insert(rectCenter) local rectRight = display.newRect(25, 10, 10, 20) rectRight:setFillColor( 0,1,1 ) snapshot.group:insert(rectRight) snapshot.x = display.contentCenterX + math.random(-100, 100) snapshot.y = display.contentCenterY + math.random(-100, 100) return snapshot end local function formatUsage(value) if (value \< 1e3) then return value .. " Kb" end value = value / 1024 return value .. " Mb" end local function PrintMemoryUsage() local usage = system.getInfo( "textureMemoryUsed" ) / 1024 -- to Kb local memUsed = collectgarbage("count") print("texture memory used", formatUsage(usage)) print("system memory used", formatUsage(memUsed)) end local function createSnapshotGroup() local count = math.random(200, 300) local parent = display.newGroup() local snapshots = {} for i = 1, count do snapshots[i] = createSnapshot(parent) end timer.performWithDelay(1400, function() for i = 1, #snapshots do parent:removeSelf() local snapshotToRemove = snapshots[i] snapshotToRemove:removeSelf() --uncomment this lines to prevent memory leak --for j = snapshotToRemove.group.numChildren, 1, -1 do -- snapshotToRemove.group[j]:removeSelf() --end end PrintMemoryUsage() collectgarbage("collect") end) end timer.performWithDelay(1500, createSnapshotGroup, -1)

You can put it into main.lua and run - see memory output in console.

From documentation
 

The snapshot group behaves just like a normal group except:

  • This group cannot be inserted into another group.
  • This group cannot be removed, for example via removeSelf().
  • This group has no parent. - so this probably means that group childs will never be removed automatically