主页 > 编程资料 > Delphi >
发布时间:2015-09-22 作者:网络 阅读:89次

 
//工程文件中加入:

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.

关键字词: