Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
519 views
in Technique[技术] by (71.8m points)

android - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...