我正在打开一个新表格。每次打开它时都变慢了。我尝试了 FormClose 事件 FreeAndnil、Free、Release、DisposeOf 但没有解决方案。我添加了一个待机计时器:
Button1 第一次点击,Form 2 显示 0,18 秒后
Button1 第二次点击,Form 2 显示 0,20 秒后
Button1 第三次点击,Form 2 显示 0,23 秒后
Button1 第四次点击,Form 2 显示 0,28 秒后 . . .
Button1 xxxx 单击,Form 2 显示后 6,30 秒
//Form1 Button1 Click
Application.CreateForm(TFrom2,Form2);
Form2.Show;
//Form2 OnCLose
//i try
//FreeAndNil, Free, Relsease,DisposeOf ...
如何解决? 示例项目来源: https://www.dropbox.com/s/yeqpizr6rfo6254/LeakProblem.rar
尝试 20-30 次点击“Form2 显示按钮” 请参阅备忘录中的增加等待时间。
最佳答案
好吧,我把你的项目搞砸了一点,让它工作了,但是有很多事情你做的和我通常的编程方式不同。
对于初学者来说,在移动端处理表单时,您需要使用 .DisposeOf,而不是 FreeAndNil。参见 this link ,尤其是:
there are scenarios when you need to execute the destructor code right away, regardless of the fact that there might be other pending references to the object. [...] the new compiler introduces a dispose pattern:
MyObject.DisposeOf
;
我注意到的另一件事是您的 Unit3 创建了 Form4。但是你让第 4 单元访问第 3 单元中的备忘录。我希望这只是为了演示目的,因为我认为不建议采用这种设计。
所以,我是如何让它工作的简短总结:我在 Form4 中设置了一个 TNotifyEvent
,以便在它完成时使用:
procedure TForm4.Button1Click(Sender: TObject);
begin
if assigned(FOnCloseEvent) then
FOnCloseEvent(Self);
end;
在 Uni3 中,我在创建表单时设置了一个处理程序:
MyForm.OnCloseEvent := CloseEvent;
事件触发这段代码:
procedure TForm3.CloseEvent(Sender: TObject);
begin
if assigned(MyForm) then
MyForm.DisposeOf;
end;
我还使 MyForm
成为 TForm3
的私有(private)对象,而不是局部变量。
通过这些更改,我可以随时点击显示和关闭,并且表单将始终快速显示。可能有更好的方法来做到这一点,如果有请告诉我!
关于android - Delphi Android 内存泄漏(或其他减速),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21880046/