Ian*_*oyd 3 delphi delphi-7 delphi-5 fastmm
我有一些代码,我没有写,但有一个内存泄漏.真正的奇怪之处在于,如果我在返回结构之前将结构归零,则内存只会泄漏.
泄漏在Delphi 5和Delphi 7中是可重现的.
首先我们有一个结构:
type
TLocalFile = packed record
FileName: AnsiString;
end;
Run Code Online (Sandbox Code Playgroud)
此结构是CollectionItem对象的私有成员:
TEntry = class(TCollectionItem)
private
FLocalFile: TLocalFile;
end;
Run Code Online (Sandbox Code Playgroud)
然后我们拥有拥有的集合,它具有可以返回填充结构的函数:
TEntries = class(TCollection)
protected
function GetLocalFile: TLocalFile;
public
procedure DoStuff;
end;
Run Code Online (Sandbox Code Playgroud)
由于功能中的怪异GetLocalFile:
function TEntries.GetLocalFile: TLocalFile;
var
s: AnsiString;
begin
//Only leaks if i initialize the returned structure
// FillChar(Result, SizeOf(Result), 0);
ZeroMemory(@Result, SizeOf(Result));
s := 'Testing Leak';
Result.Filename := s; //'Testing leak'; only leaks if i set the string through a variable
end;
Run Code Online (Sandbox Code Playgroud)
实际上,这个函数被传递给一个流,并返回一个填充的结构,但现在这并不重要.
接下来我们有一个集合的方法,它将填充它的所有条目的LocalFile结构:
procedure TEntries.DoStuff;
var
x: Integer;
begin
for X := 0 to Count-1 do
begin
(Items[X] as TEntry).FLocalFile := GetLocalFile;
end;
end;
Run Code Online (Sandbox Code Playgroud)
最后,我们构建一个集合,添加10个项目,拥有它们DoStuff,然后释放列表:
procedure TForm1.Button1Click(Sender: TObject);
var
list: TEntries;
i: Integer;
entry: TCollectionItem;
begin
list := TEntries.Create(TEntry);
try
for i := 1 to 10 do
entry := list.Add;
list.DoStuff;
finally
list.Free;
end;
end;
Run Code Online (Sandbox Code Playgroud)
我们创建了10个项目,我们泄漏9个 AnsiStrings.
有些方法可以使代码不泄漏.它只在使用中间字符串堆栈变量时泄漏
更改:
function TEntries.GetLocalFile: TLocalFile;
var
s: AnsiString;
begin
s := 'Testing Leak';
Result.Filename := s; //'Testing leak'; only leaks if i set the string through a variable
end;
Run Code Online (Sandbox Code Playgroud)
至
function TEntries.GetLocalFile: TLocalFile;
begin
Result.Filename := 'Testing leak'; //doesn't leak
end;
Run Code Online (Sandbox Code Playgroud)
它不会泄漏.
另一种方法是在返回结构之前不初始化结构:
删除对FillChar或的调用ZeroMemory,它不会泄漏:
function TEntries.GetLocalFile: TLocalFile;
var
s: AnsiString;
begin
//Only leaks if i initialize the returned structure
// FillChar(Result, SizeOf(Result), 0);
// ZeroMemory(@Result, SizeOf(Result));
s := 'Testing Leak';
Result.Filename := s; //'Testing leak'; only leaks if i set the string through a variable
end;
Run Code Online (Sandbox Code Playgroud)
这些是奇怪的决议.无论我是否使用中间堆栈变量,无论是否将结构归零,都不应对内存清理产生任何影响.
我怀疑这是编译器中的一个错误.这意味着我(意思是写这个的人)做了一些根本错误的事情.我认为它与某些事情有关TCollectionItemClass.但我不能为我的生活弄清楚什么.
unit FMain;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TLocalFile = packed record
FileName: AnsiString;
end;
TEntry = class(TCollectionItem)
private
FLocalFile: TLocalFile;
end;
TEntries = class(TCollection)
protected
function GetLocalFile: TLocalFile;
public
procedure DoStuff;
end;
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
public
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
uses
contnrs;
procedure TForm1.Button1Click(Sender: TObject);
var
list: TEntries;
i: Integer;
entry: TCollectionItem;
begin
list := TEntries.Create(TEntry);
try
for i := 1 to 10 do
begin
entry := list.Add;
end;
list.DoStuff;
finally
list.Free;
end;
end;
{ TEntries }
procedure TEntries.DoStuff;
var
x: Integer;
entry: TEntry;
begin
for X := 0 to Count-1 do
begin
entry := Items[X] as TEntry;
entry.FLocalFile := GetLocalFile;
end;
end;
function TEntries.GetLocalFile: TLocalFile;
var
s: AnsiString;
begin
//Only leaks if i initialize the returned structure
// FillChar(Result, SizeOf(Result), 0);
ZeroMemory(@Result, SizeOf(Result));
s := 'Testing Leak';
Result.Filename := s; //'Testing leak'; only leaks if i set the string through a variable
end;
end.
Run Code Online (Sandbox Code Playgroud)
哦,不要忘记添加FastMM4到您的项目中(如果您还没有内置),这样您就可以检测到泄漏.

AnsiString(及其Unicode对应物)由编译器引用计数.你不能简单地将包含引用的内存归零; 您需要分配''给它,以便编译器生成代码以减少引用计数并正确释放内存.
尝试阻止清除包含对动态数组,接口或(某些)变体的引用的数据结构时,您会遇到类似的问题.
如果您没有使用最近足以支持Default编译器魔术表达的Delphi版本(我相信它是在D2009中引入的)Finalize,那么安全地清除记录的最佳方法是先调用,然后将内存归零'干嘛.