How to release a Firemonkey control properly, in t

2019-04-09 22:10发布

问题:

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?

回答1:

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.Synchronize(nil,
    procedure
    begin
      Self.DisposeOf;
    end);
  end).Start;
end;

Instead of Synchronize you can also use Queue in above method.

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.