Can't remove an image created inside an enterFrame event

I have a runtime enterEvent listener setup, and inside it, under certain condition, I will generate some image objects, and then outside this event callback function and within the same lua file, I have another function that would try to remove the image objects that were created using display.remove api, and the image creation api I used is display.newImageRect.

I would really appreciate it if someone could point out the problem for me as I scratched my head and kind got stuck here for some time.

Here are some code snips from my gameBoard.lua file:

local tileMap = {}

local clear

local options

local sheet

local function onFrame( event )

tileMap[col][row] = display.newImageRect( sheet, number, 32, 32)

end

function clear()

display.remove(tileMap[col][row])

tileMap[col][row] = nil

end

local function init()

    options =

    {

        width = 32,

        height = 32,

        numFrames = 64,

        sheetContentWidth = 256,  --width of original 1x size of entire sheet

        sheetContentHeight = 256  --height of original 1x size of entire sheet

    }

    sheet = graphics.newImageSheet( “TestBlocks.bmp”, options )

    Runtime:addEventListener( “enterFrame”, onFrame )    

end

I figured it out. I think I was doing double work in my project, when an old object (image) moved, I created another object( image) in the new location, but didn’t remove the old object handle before I used it to create next new object. So after I free the old object, the problem went away. However, I think I can optimize my code a bit more, by just reassign the new location to reference the old object, and save one creation.

I figured it out. I think I was doing double work in my project, when an old object (image) moved, I created another object( image) in the new location, but didn’t remove the old object handle before I used it to create next new object. So after I free the old object, the problem went away. However, I think I can optimize my code a bit more, by just reassign the new location to reference the old object, and save one creation.