RSS订阅 | 匿名投稿
您的位置:网站首页 > 内存 > 正文

delphi2010 的自带的内存泄漏检测

作者:admin 来源: 日期:2011/8/11 10:51:34 人气: 标签:

 
//工程文件中加入:

program Project1;

uses
Forms,
Unit1 in 'Unit1.pas' {Form1};

{$R *.res}

begin
Application.Initialize;
ReportMemoryLeaksOnShutdown := True; //加入这句来检查内存泄漏
Application.MainFormOnTaskbar := True;
Application.CreateForm(TForm1, Form1);
Application.Run;
end.



//单元文件

unit Unit1;

interface

uses

Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs;

type
TTest = record
par: Integer;
end;

PTest = ^TTest;
type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{ Private declarations }
public
{ Public declarations }
List: TList;
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
var
i: Integer;
begin

for
i := List.Count - 1 downto 0 do
Dispose(List.Items[i]); // 如果只用 List.Free 而没有这步将会有内存泄漏

List.Free;
end;

procedure TForm1.FormCreate(Sender: TObject);
var
p: PTest;
i: Integer;
begin
List := TList.Create;
for i := 0 to 5000 do
begin

New(p);
p.par := 55555555;
List.Add(p);
end;

end;

end.

读完这篇文章后,您心情如何?
0
0
0
0
0
0
0
0
本文网址: