[RESOLVED] Corona Simulator Crashes on object:remove call

Hey Guys!
I am developing a new (quite large) APP, and while writing my code I fell down into some nasty Corona Simulator crashes. I am a newbie in Corona and Lua, I made a simple, yet hard to find, mistake and I want to share it with y’all (Google engine included). Maybe someone else won’t waste precious time.

Look at the code below and try uncomment the “onPress” event, while commenting out the “onRelease”.

The Simulator will crash, definately.
Why?

Some expert here will give the deep right reason, but I think because the “onPress” event handles requests while the object is still “in use”. On the other hand “onRelease” is fired when the object is “free”, so you can remove it safely.

local ui = require(“ui”)

function CreateRectangle()
local localDisplay
local localRectangle
local buttonDelete

local buttonDelete_Press = function(event)
localDisplay:remove(localRectangle)
localDisplay:remove(buttonDelete)
end

localDisplay = display.newGroup()
localRectangle = display.newRect(100, 100, 100, 100)
localRectangle.strokeWidth = 3
localRectangle:setFillColor(140, 140, 140)
localRectangle:setStrokeColor(180, 180, 180)
localRectangle:setReferencePoint( display.TopLeftReferencePoint )
localDisplay:insert(localRectangle)

buttonDelete = ui.newButton {
x = 150,
y = 50,
default = “buttonDelete.png”,
text = “delete”,
– onPress = buttonDelete_Press,
onRelease = buttonDelete_Press,
emboss = true
}
localDisplay:insert(buttonDelete)

return localDisplay
end

local myDisplay = display.newGroup()
local myRectangle = CreateRectangle()
myDisplay:insert(myRectangle)

SAVE YOUR TIME !!!
DAMN… :slight_smile:

[import]uid: 86439 topic_id: 26446 reply_id: 326446[/import]