gpt4 book ai didi

Delphi字符串泄漏

转载 作者:行者123 更新时间:2023-12-03 14:49:31 25 4
gpt4 key购买 nike

我正在使用 Delphi XE,并编写一个使用 RemObjects SDK 进行通信的应用程序(如果可能相关)。我打开了 FastMM 调试,有时(并非总是)当我关闭它时,它会发出有关单个“意外内存泄漏”的警告。 “发生了意外的内存泄漏。意外的小块泄漏为:117-124 字节:UnicodeString x 1”。偶尔,我会收到 x2 报告。

现在,我的理解是字符串是引用计数的,并且由于没有其他对象涉及导致泄漏,那么可能导致这种情况发生的情况是什么?在 this StackOverflow question人们找不到泄漏的方法。

如果没有明显的办法,那么我会下载最新的FastMM源(它似乎不包含在XE源中)。

[解决后编辑] 找到此问题的解决方案是安装 FastMM 源,并启用 FullDebugMode 来获取堆栈跟踪。

最佳答案

当使用类型化常量并根据最终确定顺序时,FastMM 过去可能会报告泄漏,而实际上并不存在泄漏。

FastMM: Leaked memory reported where I believe it shouldn't.

In short, when the FinalizedFirst unit get's finalized, the SString constant get's freed. After finalization of the unit is done, the finalization of FinalizedLast get's called. In it is finalization, it call's the method LeakMemory of the FinalizedFirst method. The SString variable gets initialized again and doesn't get freed as the finalization of FinalizedFirst has already run.

最终确定的单元

unit FinalizedLast;    

interface

uses FinalizedFirst;

implementation

initialization LeakMemory;
finalization LeakMemory;
end.

最终确定的第一个单元

unit FinalizedFirst;

interface

procedure LeakMemory;

implementation

uses FinalizedLast;

procedure LeakMemory;
const
SString: string = '';
begin
//***** SString will get initialized once or twice depending on the
// finalization order of units. If it get's initialized twice,
// a memory leak is reported.
if SString = '' then
SString := 'FooBar';
end;
end.

项目泄漏内存

program LeakMemory;
uses
FastMM4 in 'FastMM4.pas',
Forms,
FinalizedFirst in 'FinalizedFirst.pas',
FinalizedLast in 'FinalizedLast.pas';

{$R *.RES}
begin

Application.Initialize;
Application.Run;

end.

关于Delphi字符串泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5423329/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com