如何正确释放Firemonkey控件(在这种情况下为带有父级的子窗体)? [英] How to release a Firemonkey control properly, in this case a child form with a parent?

查看:208
本文介绍了如何正确释放Firemonkey控件(在这种情况下为带有父级的子窗体)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从控件本身的事件处理程序中删除并释放它.

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

TFmxObject.Release的典型用例,不是吗?但是,它似乎只能在Windows上运行,而不能在Android上运行,并且无论如何现在都已弃用此方法.

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.

我知道,不起作用并不是一个很好的问题描述,但是目前我无法在android下调试它.在Windows下,我看到事件处理程序在.Release之后继续正确运行,并且在完成后,执行了我的控件析构函数中的日志消息.在Android下,该应用程序会挂起.

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.

当我改用.Free时,它仍然可以在Windows下运行(析构函数立即发生,但是释放后处理程序不会访问控件),并且在Android中没有可见的问题,但是从未调用析构函数,所以我有泄漏.

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.

使用.DisposeOf的效果与使用.Release的效果相同-Windows正常,Android挂起.

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

我也尝试过MyParent.RemoveComponent(MyControl),但都无济于事.

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

要释放所有引用,ARC还可以做些什么?还是其他呢?

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

推荐答案

TFmxObject.Release在内部使用TThread.ForceQueue,目前在Android下已被破坏(请参见上面的讨论).

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;

您也可以在上述方法中使用Queue代替Synchronize.

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.

这篇关于如何正确释放Firemonkey控件(在这种情况下为带有父级的子窗体)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆