App crashes on device whenever I remove a snapshot

My app crashes everytime it tries to remove a snapshot. It happens on the device (android), but not while running on Simulator.


-- Invalidates snapshot at every frame
local function invalidateSnapshot()
	snapshot.invalidate()
end

-- Called when removing the snapshot
function removeSnapshot()
	
	-- Stopping the update of the snapshot
	Runtime:removeEventListener("enterFrame", invalidateSnapshot)
	
	-- Removing the snapshot
	if snapshot ~= nil then
		if snapshot.group ~= nil then
			snapshot.group:removeSelf()
			snapshot.group = nil
		end
		snapshot:removeSelf()
		snapshot = nil
	end

end

Here’s the error:
WARNING: script.lua:18: the ‘group’ property of snapshot objects is read-only

snapshot:removeSelf() seems to be the reason it crashes. There’s a function that invalidates that snapshot onEnterFrame, but I do stop it right before removing the snapshot.

Weird thing is that I can’t find a single thing on google about “snasphot boject being read-only”.

This is referring to Group in snapshot

Wow, I can’t believe I missed this documentation page :sweat_smile:

But then, what would be the proper way to remove/clear a snapshot? Should I move the content of snapshot.group to something else and simply then set snapshot = nil?

So far, I’ve removed this part:

if snapshot.group ~= nil then
	snapshot.group:removeSelf()
	snapshot.group = nil
end

Unfortunately, the app still crashes and goes instantly back to the launcher but this time, without returning any error on the Corona Console.

EDIT: It seems to work now and doesn’t crash (don’t know since I haven’t updated anything, but well…). My question still remaines: what’s the propert way to remove/clear a snapshot?

I always do this with both canvas and snapshots.

local snap = display.newSnapshot(display.actualContentWidth, display.actualContentHeight);

snap.x = display.contentCenterX

snap.y = display.contentCenterY

snap.clearColor = {0, 1, 0}

local rects = {}

for i = 1, 10, 1 do

    local rect = display.newRect(math.random(-100, 100), math.random(-100, 100), math.random(10, 32), math.random(10, 32))

    rect:setFillColor(math.random(), math.random(), math.random(), 1);

   

    rects[i] = rect

    snap.group:insert(rect)

end

timer.performWithDelay(200, function ()

    local idx = #rects

    local rect=  rects[idx]

    display.remove(rect)

    rect = nil

    table.remove(rects, idx)

    snap:invalidate()

    print(snap.group.numChildren)

   

    if snap.group.numChildren <= 0 then

        snap:removeSelf()

        snap = nil

    end

    print(tostring(snap))

end, 10);

Put all children into tables, and remove them if snapshots need to be deleted.

    for i = #rects, 1, -1 do
        local rect = rects[i]
        if rect then
            rect:removeSelf()
            table.remove(rects, i)
        end
    end

    snap:removeSelf()
    snap = nil

    rects = nil

Thank you a lot for your replies, helps a lot!

Just one more question about your way: do you really need to remove every rectangle inserted into snap.group as the group should behave like a normal group? As far as I know, removing a group also remove every other item inserted into it (… please, tell me it does or I’m gonna have a very bad week lol).

Yeah, snap group will remove all children together.
My way is to make sure in case you need to change the drawn child objects.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.