版块导航
正在加载中...
客户端APP下载
登录
注册
帖子
帖子
用户
本版
应《网络安全法》要求,自2017年10月1日起,未进行实名认证将不得使用互联网跟帖服务。为保障您的帐号能够正常使用,请尽快对帐号进行手机号验证,感谢您的理解与支持!
24小时热门版块排行榜
>
虫友互识
(5909)
>
论坛更新日志
(4209)
>
留学DIY
(3104)
>
留学生活
(2462)
>
海外博后
(1504)
>
育儿交流
(1202)
>
医药类考试
(1001)
>
海归之家
(1000)
>
材料综合
(799)
>
人文社科
(798)
>
信息科学
(760)
>
复合材料
(658)
>
地学
(640)
>
分子模拟
(601)
>
文学芳草园
(601)
>
转基因
(600)
小木虫论坛-学术科研互动平台
»
计算模拟区
»
程序语言
»
Delphi&Pascal
»
【转帖】delphi中如何检测内存透露(null)
1
1/1
返回列表
查看: 533 | 回复: 0
只看楼主
@他人
存档
新回复提醒
(忽略)
收藏
在APP中查看
zyj8119
木虫
(著名写手)
应助: 65
(初中生)
贵宾: 0.003
金币: 915.1
散金: 1440
红花: 35
帖子: 2936
在线: 1329.4小时
虫号: 664177
注册: 2008-11-29
性别: GG
专业: 理论和计算化学
[交流]
【转帖】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.
回复此楼
» 猜你喜欢
filecode=后面第一个是大写字母
已经有7人回复
时间戳又变了8-15
已经有22人回复
欢迎发来filecode的Mz6后的代码验证其规律
已经有60人回复
有时候,自然基金真的不能太认真 (我的申报经验)
已经有10人回复
filecode
已经有12人回复
小木虫上这么多卖论文的,真有人买论文么?感觉没必要啊
已经有13人回复
哪位老哥知道今年的国自然具体哪一天放榜?
已经有7人回复
关于Filecode分析方法
已经有13人回复
各位道友,我要去昆明玩几天,回来见。
已经有8人回复
咱们一起用铁证分析2026国家社科基金中标与否
已经有22人回复
高级回复
好好学习,天天向上。
1楼
2010-12-02 16:29:32
已阅
回复此楼
关注TA
给TA发消息
送TA红花
TA的回帖
相关版块跳转
第一性原理
量子化学
计算模拟
分子模拟
仿真模拟
程序语言
我要订阅楼主
zyj8119
的主题更新
1
1/1
返回列表
如果回帖内容含有宣传信息,请如实选中。否则帐号将被全论坛禁言
普通表情
龙
兔
虎
猫
高级回复
(可上传附件)
百度网盘
|
360云盘
|
千易网盘
|
华为网盘
在新窗口页面中打开自己喜欢的网盘网站,将文件上传后,然后将下载链接复制到帖子内容中就可以了。
最具人气热帖推荐
[查看全部]
作者
回/看
最后发表
[
基金申请
]
filecode=后面第一个是大写字母
+6
wangze12014
2026-08-14
7/350
2026-08-15 20:12
by
gltch
[
基金申请
]
时间戳又变了8-15
+12
archvillain
2026-08-15
22/1100
2026-08-15 19:55
by
医学老男孩
[
基金申请
]
欢迎发来filecode的Mz6后的代码验证其规律
+28
医学老男孩
2026-08-13
60/3000
2026-08-15 19:54
by
医学老男孩
[
基金申请
]
有时候,自然基金真的不能太认真 (我的申报经验)
+8
majunge000
2026-08-11
10/500
2026-08-15 18:55
by
Equinoxhua
[
基金申请
]
filecode
+8
cratir
2026-08-14
12/600
2026-08-15 18:08
by
zyfgau
[
基金申请
]
小木虫上这么多卖论文的,真有人买论文么?感觉没必要啊
+12
Tide man
2026-08-10
13/650
2026-08-15 16:34
by
氺木
[
基金申请
]
哪位老哥知道今年的国自然具体哪一天放榜?
+7
Ldrop2023
2026-08-13
7/350
2026-08-15 15:57
by
mzhh2000
[
基金申请
]
各位道友,我要去昆明玩几天,回来见。
+7
Tide man
2026-08-14
8/400
2026-08-15 01:11
by
arzu_hma
[
基金申请
]
咱们一起用铁证分析2026国家社科基金中标与否
+7
启萌科技
2026-08-12
22/1100
2026-08-14 23:45
by
Noways
[
基金申请
]
是这周出结果还是下周出结果?
+4
yuleib84
2026-08-11
4/200
2026-08-14 23:05
by
lfy8008
[
基金申请
]
应该是93bebmhtak前后十一个字符比较关键
+23
Lanmanbaby
2026-08-09
37/1850
2026-08-14 13:40
by
Equinoxhua
[
硕博家园
]
请教兼职经验
+3
是阿文鸭
2026-08-09
3/150
2026-08-14 12:07
by
HER12025
[
硕博家园
]
读博的好处
+4
lnee
2026-08-11
4/200
2026-08-14 10:20
by
ahsoarli
[
基金申请
]
filecode
+15
documentary
2026-08-10
17/850
2026-08-14 10:08
by
kissu88
[
文学芳草园
]
阿姨
+4
汪汪锅
2026-08-09
4/200
2026-08-13 19:43
by
arzu_hma
[
基金申请
]
分享一下我之前已中青C的计划书的filecode
+4
布布和一二
2026-08-11
5/250
2026-08-13 12:56
by
cratir
[
基金申请
]
结合人工智能,周易传统文化,filecode打分制来了,3分以上希望很大。
+3
Tide man
2026-08-12
4/200
2026-08-13 08:35
by
ZJTJZ
[
基金申请
]
什么时候出结果,有咨询渠道???
+3
Tide man
2026-08-11
3/150
2026-08-11 17:54
by
kudofaye
[
基金申请
]
据悉今年马上要出结果了
+7
瞬息宇宙
2026-08-10
8/400
2026-08-10 12:42
by
Vivilian
[
基金申请
]
面上项目filecode邪修
+5
西山十月
2026-08-09
7/350
2026-08-10 07:32
by
仁砚薪传
信息提示
关闭
请填处理意见
关闭
确定