首页 > 代码库 > 教程-在Delphi中怎么查看是否有内存泄漏(Delphi2007)+WIN7
教程-在Delphi中怎么查看是否有内存泄漏(Delphi2007)+WIN7
相关资料:
1.http://bbs.csdn.net/topics/390630932?page=1
PS:
1.本实例D2007及以上版本支持。
2.检测内存工具 EurekaLog fastmm
实例代码:
Project1.dpr:
1 program Project1; 2 3 uses 4 Forms, 5 Unit1 in ‘Unit1.pas‘ {Form1}; 6 7 {$R *.res} 8 9 begin10 Application.Initialize;11 ReportMemoryLeaksOnShutdown := True; //加入该行,在程序运行并关闭后会提示你什么对象在哪里泄露了12 Application.MainFormOnTaskbar := True;13 Application.CreateForm(TForm1, Form1);14 Application.Run;15 end.
Unit1.pas:
1 unit Unit1; 2 3 interface 4 5 uses 6 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, 7 Dialogs, StdCtrls; 8 9 type10 TForm1 = class(TForm)11 Button1: TButton;12 procedure Button1Click(Sender: TObject);13 private14 { Private declarations }15 public16 { Public declarations }17 end;18 19 var20 Form1: TForm1;21 22 implementation23 24 {$R *.dfm}25 26 procedure TForm1.Button1Click(Sender: TObject);27 var28 oForm2: TForm1;29 begin30 oForm2 := TForm1.Create(nil);31 try 32 finally33 //oForm2.Free;34 end;35 end;36 37 end.
运行效果:
---------------------------
Unexpected Memory Leak
---------------------------
An unexpected memory leak has occurred. The unexpected small block leaks are:
1 - 12 bytes: Unknown x 3
13 - 20 bytes: TList x 4, String x 2, Unknown x 3
21 - 28 bytes: TIconImage x 1, TPen x 1, TBrush x 3
29 - 36 bytes: TPadding x 2, TMargins x 2, TSizeConstraints x 2, TFont x 3, Unknown x 2
37 - 44 bytes: TGlassFrame x 1
45 - 52 bytes: TIcon x 1
61 - 68 bytes: Unknown x 1
69 - 76 bytes: TControlScrollBar x 2
93 - 100 bytes: TControlCanvas x 1
573 - 620 bytes: TButton x 1
797 - 876 bytes: TForm1 x 1
---------------------------
确定
---------------------------
教程-在Delphi中怎么查看是否有内存泄漏(Delphi2007)+WIN7