在 Delphi 中,我已配置为报告内存泄漏:
{$IFDEF Debug}
ReportMemoryLeaksOnShutdown := true;
{$ENDIF}
After exiting the program I get the following message:
![enter image description here](https://i.stack.imgur.com/D8e17.png)
我的程序非常大,我不知道如何找到我创建但未释放的 TStringList。
不幸的是Delphi没有垃圾收集器......
如果我在我的项目中搜索TStringList
我发现大约有 500 次出现。检查所有这些没有任何意义。
如何找到我忘记释放的变量?
以下软件有助于查找内存泄漏:
https://github.com/shadow-cs/delphi-leakcheck
以下是要遵循的步骤:
-
cd C:\Delphi
git clone https://github.com/shadow-cs/delphi-leakcheck
- 添加路径
C:\Delphi\delphi-leakcheck\Source
到您的 Delphi 库路径(工具/选项/...)
- 在你的开头添加以下代码
MyProject.dpr
file:
Code:
uses
// LeakCheck, // Does not need do be defined here (LeakCheck.Report will do it) unless you want to reference it from the DPR
LeakCheck.Report, // Me first! - I don't have any dependencies but LeakCheck so I finalize after all other units
LeakCheck.Setup.Trace, // (Optional) Then me - Run setup to configure stack tracing for us
LeakCheck.Report.FileLog, // Then me - I'm the one that pulls some dependencies and have all the functionality
- 编译并启动你的程序
- 定期退出程序
退出可能需要长达几分钟(请耐心等待......)
- You will get a message box that tells you that a file
C:\Delphi\MyProject\MyProject.log
was created.
This log file contains the name of the function (and its callstack) where the variable was created. It's not the name of the unfreed variable, but at least the function and line number inside this function.
![enter image description here](https://i.stack.imgur.com/WErqG.png)
这种方法在几分钟内解决了我的问题。
奖励:该软件完全免费 - 您无需为其支付任何费用
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)