Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to release a Firemonkey control properly, in this case a child form with a parent?

From inside an event handler of the control itself, I would like to delete and free it.

A typical use case for TFmxObject.Release, isn't it? However, it only seems to work under Windows, but not Android, and this method is now deprecated anyway.

I know, doesn't work is not a good problem description, but currently I'm not able to debug it under android. Under Windows, I see that the event handler continues correctly after the .Release and after it finished, my log message inside my controls destructor is executed. Under Android, the application hangs.

When I use .Free instead, it still works under Windows (destructor happens immediately, but the handler doesn't access the control after the free), and in Android there is no visible problem, but the destructor is never called, so I have a leak.

With .DisposeOf the effect is the same as with .Release - Windows ok, Android hangs.

I also tried MyParent.RemoveComponent(MyControl) but it all didn't help.

What else do I have to do to release all references so that ARC can do its work? Or how else?

like image 330
maf-soft Avatar asked Mar 06 '23 17:03

maf-soft


1 Answers

TFmxObject.Release uses TThread.ForceQueue internally, and that's currently broken under Android (see discussion above).

As a workaround, a working cross-platform version for releasing an object from its event handler would be

procedure TForm.CloseBtnClick(Sender: TObject);
begin
  Parent := nil;
  TThread.CreateAnonymousThread(
  procedure
  begin
    TThread.Queue(nil,
    procedure
    begin
      Self.DisposeOf;
    end);
  end).Start;
end;

What is important to keep in mind is that you should not keep any other references to the control you are releasing or you may hit the trouble down the road.

Update for 10.4 and newer versions:

Since 10.4 and unified memory management DisposeOf is now equivalent to Free on all platforms and Self.DisposeOf can be replaced with Self.Free.

like image 93
Dalija Prasnikar Avatar answered Mar 09 '23 07:03

Dalija Prasnikar