Search code examples
delphieventscontrolsfiremonkeyfree

How to free a control in its own event?


I have a list of TPanels in a FMX application and I want to free a panel if I click on it.

To free them directly in the Onclick-handler is not the right way, because I get an access-violation. And I don't want to use windowsmessages (recommended in How to free control inside its event handler? and Why does my program crash when I destroy a button in its own OnClick handler?) because it is a firemonkey application and I do not know how these messages work on android and mac.

Is there another solution?


Solution

  • Use myObject.Release:

    Marks this TFmxObject object for delayed deletion.

    Immediate actions in this method:

    • set Parent = nil
    • insert object into delayed delete list

    Delayed action:

    • free object from list (vPurgatory).

    Remember, that method Free (and procedure FreeAndNil) does not remove the object itself in mobile platforms:

    // under ARC, this method isn't actually called since the compiler translates // the call to be a mere nil assignment to the instance variable, which then calls _InstClear