delphi2010 的自带的内存泄漏检测


本文整理自网络,侵删。

  
//工程文件中加入:

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.

相关阅读 >>

Delphi 使用 idhttp 获取 utf-8 编码的中文网页

Delphi xe7中stringgrid组件的使用

Delphi dbgrid中实现copy、paste功能

Delphi case 语句中使用字符串

Delphi xe 获取 android application version 版本

Delphi 如何从dll中检索导出函数的列表

Delphi checklistbox简单用法

Delphi添加任务栏右键菜单

Delphi 突破主动防御

Delphi 关于汉字换行问题

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...