Clearing image tables crashes the simulator/console

Okay I'm super confused about that one.
I coded a simple function to make an mirrored image table at run time, to save storage space on animations.
Now the issue is, if I later clear any image table made with this function (imagetable = nil) to get some memory back, the console crashes. I really can't figure that one out. Anyone has any idea?
Here's the function mirroring an image table:

function mirrorImageTable(imageTable)
local length = imageTable:getLength()
local mirroredImageTable = gfx.imagetable.new(length)
local firstImage = imageTable:getImage(1)
local xSize, ySize = firstImage:getSize() -- Gets image size from first image
for n = 1, length -- Mirrors frames
do
local image = gfx.image.new(xSize, ySize, gfx.kColorBlack)
local imageMask = gfx.image.new(xSize, ySize, gfx.kColorBlack)
gfx.pushContext(image)
gfx.setImageDrawMode(gfx.kDrawModeCopy) -- Normal copy mode
imageTable:drawImage(n, 0, 0, gfx.kImageFlippedX) -- Draws flipped image
gfx.popContext()
gfx.pushContext(imageMask)
gfx.setImageDrawMode(gfx.kDrawModeFillWhite) -- Fillwhite mode for mask
imageTable:drawImage(n, 0, 0, gfx.kImageFlippedX) -- Draws mask flipped
gfx.popContext()
image:setMaskImage(imageMask) -- Assigns mask
mirroredImageTable:setImage(n, image) -- Sets mirrored image with mask
end
return mirroredImageTable
end

Anything in the crash log?

The simulator says:

System Integrity Protection: enabled

Crashed Thread: 0 Dispatch queue: com.apple.main-thread

Exception Type: EXC_BAD_ACCESS (SIGSEGV)
Exception Codes: EXC_I386_GPFLT
Exception Note: EXC_CORPSE_NOTIFY

Termination Signal: Segmentation fault: 11
Termination Reason: Namespace SIGNAL, Code 0xb
Terminating Process: exc handler [3953]

I have no idea what this means! :slight_smile:

I was able to reproduce the crash with your code. It seems like an SDK bug to me.

Here is a version of the function that seems to work and doesn't crash when the memory is freed later


function mirrorImageTable(imageTable)
    local length = imageTable:getLength()
    local mirroredImageTable = gfx.imagetable.new(length)
    local firstImage = imageTable:getImage(1)
    local xSize, ySize = firstImage:getSize() -- Gets image size from first image
    for n = 1, length -- Mirrors frames
    do
    local image = gfx.image.new(xSize, ySize, gfx.kColorClear)
    gfx.pushContext(image)
    gfx.setImageDrawMode(gfx.kDrawModeCopy) -- Normal copy mode
    imageTable:drawImage(n, 0, 0, gfx.kImageFlippedX) -- Draws flipped image
    gfx.popContext()
    gfx.pushContext(image:getMaskImage())
    gfx.setImageDrawMode(gfx.kDrawModeFillWhite) -- Fillwhite mode for mask
    imageTable:drawImage(n, 0, 0, gfx.kImageFlippedX) -- Draws mask flipped
    gfx.popContext()
    mirroredImageTable:setImage(n, image) -- Sets mirrored image with mask
    end
    return mirroredImageTable
end

That’s interesting! Thank you for that. So the difference seems that you’re setting up the image with color clear, and you get the mask from the image, instead of setting a new image as mask. Should this be moved to SDK bugs then? I can’t quite figure why this new code works.

I think it should be moved to bugs yeah. Both ways should be valid. Simply nilling a value in lua should never cause a segfault I think.

I've moved the thread to bugs

Thank you Alex and Matt