24小时热门版块排行榜    

Znn3bq.jpeg
查看: 500  |  回复: 0

zyj8119

木虫 (著名写手)

[交流] 【转帖】delphi中如何检测内存透露(null)

试试偶这个内存应用 监督 器
用法非常简略 ,在你的project source里把
利用 这个单元的那句放到最前,如
...
CODE:
uses
MemoryManager in '...pas',
Forms,
Main in 'Main.pas' {frmMain},
...
修正 自Delphi Developer's Handbook……
代码如下……
unit MemoryManager;
interface
var
GetMemCount: Integer = 0;
FreeMemCount: Integer = 0;
ReallocMemCount: Integer = 0;
var
mmPopupMsgDlg: Boolean = True;
mmSaveToLogFile: Boolean = True;
mmErrLogFile: string = '';
 
procedure SnapToFile(Filename: string);
implementation
uses
Windows, SysUtils, TypInfo;
const
MaxCount = High(Word);
var
OldMemMgr: TMemoryManager;
ObjList: array[0..MaxCount] of Pointer;
FreeInList: Integer = 0;
procedure AddToList(P: Pointer);
begin
if FreeInList > High(ObjList) then
begin
MessageBox(0, '内存管理监督 器指针列表溢出,请增大列表项数!', '内存管理监督 器', mb_ok);
Exit;
end;
ObjList[FreeInList] := P;
Inc(FreeInList);
end;
procedure RemoveFromList(P: Pointer);
var
I: Integer;
begin
for I := 0 to FreeInList - 1 do
if ObjList[I] = P then
begin
Dec(FreeInList);
Move(ObjList[I + 1], ObjList[I], (FreeInList - I) * SizeOf(Pointer));
Exit;
end;
end;
procedure SnapToFile(Filename: string);
var
OutFile: TextFile;
I, CurrFree, BlockSize: Integer;
HeapStatus: THeapStatus;
Item: TObject;
ptd: PTypeData;
ppi: PPropInfo;
begin
AssignFile(OutFile, Filename);
try
if FileExists(Filename) then
Append(OutFile)
else
Rewrite(OutFile);
CurrFree := FreeInList;
HeapStatus := GetHeapStatus; { 局部堆状态 }
with HeapStatus do
begin
writeln(OutFile, '--');
writeln(OutFile, DateTimeToStr(Now));
writeln(OutFile);
write(OutFile, '可用地址空间 : ');
write(OutFile, TotalAddrSpace div 1024);
writeln(OutFile, ' 千字节');
write(OutFile, '未提交部分 : ');
write(OutFile, TotalUncommitted div 1024);
writeln(OutFile, ' 千字节');
write(OutFile, '已提交部分 : ');
write(OutFile, TotalCommitted div 1024);
writeln(OutFile, ' 千字节');
write(OutFile, '空闲部分 : ');
write(OutFile, TotalFree div 1024);
writeln(OutFile, ' 千字节');
write(OutFile, '已分配部分 : ');
write(OutFile, TotalAllocated div 1024);
writeln(OutFile, ' 千字节');
write(OutFile, '地址空间载入 : ');
write(OutFile, TotalAllocated div (TotalAddrSpace div 100));
writeln(OutFile, '%');
write(OutFile, '整个 小空闲内存块 : ');
write(OutFile, FreeSmall div 1024);
writeln(OutFile, ' 千字节');
write(OutFile, '整个 大空闲内存块 : ');
write(OutFile, FreeBig div 1024);
writeln(OutFile, ' 千字节');
write(OutFile, '其它未用内存块 : ');
write(OutFile, Unused div 1024);
writeln(OutFile, ' 千字节');
write(OutFile, '内存管理器耗损 : ');
write(OutFile, Overhead div 1024);
writeln(OutFile, ' 千字节');
end;
writeln(OutFile);
write(OutFile, '内存对象数目 : ');
writeln(OutFile, CurrFree);
for I := 0 to CurrFree - 1 do
begin
write(OutFile, I: 4);
write(OutFile, ') ');
write(OutFile, IntToHex(Cardinal(ObjList[I]), 16));
write(OutFile, ' - ');
BlockSize := PDWORD(DWORD(ObjList[I]) - 4)^;
write(OutFile, BlockSize: 4);
write(OutFile, '($' + IntToHex(BlockSize, 4) + ')字节');
write(OutFile, ' - ');
try
Item := TObject(ObjList[I]);
// code not reliable
{ write (OutFile, Item.ClassName);
write (OutFile, ' (');
write (OutFile, IntToStr (Item.InstanceSize));
write (OutFile, ' bytes)');}
// type info technique

if PTypeInfo(Item.ClassInfo).Kind <> tkClass then
write(OutFile, '不是对象')
else
begin
ptd := GetTypeData(PTypeInfo(Item.ClassInfo));
// name, 如果是TComponent
ppi := GetPropInfo(PTypeInfo(Item.ClassInfo), 'Name');
if ppi <> nil then
begin
write(OutFile, GetStrProp(Item, ppi));
write(OutFile, ' : ');
end
else
write(OutFile, '(未命名): ');
write(OutFile, PTypeInfo(Item.ClassInfo).Name);
write(OutFile, ' (');
write(OutFile, ptd.ClassType.InstanceSize);
write(OutFile, ' 字节) - In ');
write(OutFile, ptd.UnitName);
write(OutFile, '.pas');
end
except
on Exception do
write(OutFile, '不是对象');
end;
writeln(OutFile);
end;
finally
CloseFile(OutFile);
end;
end;
function NewGetMem(Size: Integer): Pointer;
begin
Inc(GetMemCount);
Result := OldMemMgr.GetMem(Size);
AddToList(Result);
end;
function NewFreeMem(P: Pointer): Integer;
begin
Inc(FreeMemCount);
Result := OldMemMgr.FreeMem(P);
RemoveFromList(P);
end;
function NewReallocMem(P: Pointer; Size: Integer): Pointer; begin
Inc(ReallocMemCount);
Result := OldMemMgr.ReallocMem(P, Size);
RemoveFromList(P);
AddToList(Result);
end;
const
NewMemMgr: TMemoryManager = (
GetMem: NewGetMem;
FreeMem: NewFreeMem;
ReallocMem: NewReallocMem);
initialization
GetMemoryManager(OldMemMgr);
SetMemoryManager(NewMemMgr);
finalization
SetMemoryManager(OldMemMgr);
if (GetMemCount - FreeMemCount) <> 0 then
begin
if mmPopupMsgDlg then
MessageBox(0, PChar(Format('出现%d处内存漏洞: ',
[GetMemCount - FreeMemCount])), '内存管理监督 器', mb_ok);
if mmErrLogFile = '' then
mmErrLogFile := ExtractFileDir(ParamStr(0)) + '.Log';
if mmSaveToLogFile then
SnapToFile(mmErrLogFile);
end;
end.

回复此楼
好好学习,天天向上。
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
相关版块跳转 我要订阅楼主 zyj8119 的主题更新
普通表情 高级回复 (可上传附件)
最具人气热帖推荐 [查看全部] 作者 回/看 最后发表
[基金申请] 河北省自然科学基金 +5 Peterchao 2026-05-18 8/400 2026-05-24 11:58 by 晓晓爱翠翠
[基金申请] 西安交大新媒学院副院长用撤稿论文结题 +3 bjvtcliu 2026-05-24 5/250 2026-05-24 10:16 by kudofaye
[教师之家] 论文撤稿了 +3 bjvtcliu 2026-05-24 5/250 2026-05-24 10:06 by Equinoxhua
[教师之家] 某211大学教师把个人教师官方主页改成:我跑了我跑了我跑了!官宣跑路! +4 zju2000 2026-05-21 5/250 2026-05-24 09:35 by songwz
[考博] 26/27申博自荐 10+4 ZXW0202 2026-05-22 9/450 2026-05-24 08:47 by bjvtcliu
[硕博家园] 售SCI一区T0P文章,我:8.O.5.5.1.O.5.4,科目齐全,可+急 +3 hvkbtfonbv 2026-05-23 3/150 2026-05-24 08:01 by 9ps9vgkqva
[硕博家园] 售SCI一区T0P文章,我:8.O.5.5.1.O.5.4,科目齐全,可+急 +3 pmo95bazuy 2026-05-23 7/350 2026-05-24 06:35 by fpo5ljpv91
[基金申请] 揭秘青基评审内幕:几个A才能顺利中标 +3 国自然国社科中 2026-05-23 4/200 2026-05-23 15:37 by 2000zf36392
[基金申请] 青B发送上会通知了吗 +5 chemBioBro 2026-05-22 7/350 2026-05-23 12:35 by zhuifengzhy
[考博] 博士申请 +3 焦晓明 2026-05-21 3/150 2026-05-23 11:26 by mlc840311
[论文投稿] 投稿求助,期刊 +4 希冀,有书读 2026-05-20 8/400 2026-05-22 10:16 by 希冀,有书读
[文学芳草园] 献血感触 +7 呀呀好傻 2026-05-19 13/650 2026-05-21 20:15 by 呀呀好傻
[基金申请] 面上本子正文33页,违规吗?会被低分嘛? +14 1234567wang 2026-05-17 16/800 2026-05-21 17:58 by 脆脆的饼干
[基金申请] 国自然评分 +4 无名者登山 2026-05-20 5/250 2026-05-21 16:35 by swuq
[基金申请] 提交了我也来说说感想 +9 fummck 2026-05-20 10/500 2026-05-21 14:17 by draco1987
[基金申请] 评审有感 +15 popular289 2026-05-18 26/1300 2026-05-21 10:35 by 西葫芦炒鸡蛋
[有机交流] 反应很差,大量原料没有反应 5+3 Mr.Zot 2026-05-19 8/400 2026-05-20 22:19 by Equinoxhua
[考博] 如果工作了想读博,可以边工作边读全日制嘛? 30+3 铁达火车 2026-05-18 5/250 2026-05-20 09:33 by tfang
[考博] 博士申请 +5 星…… 2026-05-18 6/300 2026-05-18 23:49 by 糊糊涂涂好
[硕博家园] 我在等一个没有答案的答案 +3 Love_MH 2026-05-17 3/150 2026-05-18 02:22 by 竹林孤影
信息提示
请填处理意见