gpt4 book ai didi

delphi - 为什么 Windbg 无法看到 Delphi 中创建的内存泄漏?

转载 作者:行者123 更新时间:2023-12-02 07:57:48 25 4
gpt4 key购买 nike

正如题主所说,为什么windbg看不到delphi中分配的任何内存?例如!heap -s 没有提供任何信息,而只是为了测试目的而故意创建了 10MB 内存泄漏。

delphi如何分配内存而不从堆中获取内存?

最佳答案

!heap 使用通过调用 HeapAllocHeapReAlloc 等分配的内存。Delphi 的默认内存管理器使用 VirtualAlloc 然后实现它自己的子分配器。因此,Delphi 的内存管理器正在执行与HeapAlloc 类似的任务。但这意味着 Delphi 默认内存管理器分配的内存对 !heap 不可见。

如果您确实想使用 WinDbg 和 !heap,那么您可以将 Delphi 内存管理器替换为基于 HeapAlloc 构建的内存管理器。也许这适合您的调试要求。我不太清楚是什么驱使您使用 WinDbg 和 !heap

或者,如果您想要使用 Delphi 原生方法来查找泄漏,您可以使用 FastMM4(完整版本,而不是 Delphi 内置版本)或 madExcept 4 等工具。

作为基于 HeapAlloc 构建的简单内存管理器替换的演示,我提供此单元:

unit HeapAllocMM;

interface

implementation

uses
Windows;

function GetMem(Size: NativeInt): Pointer;
begin
Result := HeapAlloc(0, 0, size);
end;

function FreeMem(P: Pointer): Integer;
begin
HeapFree(0, 0, P);
Result := 0;
end;

function ReallocMem(P: Pointer; Size: NativeInt): Pointer;
begin
Result := HeapReAlloc(0, 0, P, Size);
end;

function AllocMem(Size: NativeInt): Pointer;
begin
Result := GetMem(Size);
if Assigned(Result) then begin
FillChar(Result^, Size, 0);
end;
end;

function RegisterUnregisterExpectedMemoryLeak(P: Pointer): Boolean;
begin
Result := False;
end;

const
MemoryManager: TMemoryManagerEx = (
GetMem: GetMem;
FreeMem: FreeMem;
ReallocMem: ReallocMem;
AllocMem: AllocMem;
RegisterExpectedMemoryLeak: RegisterUnregisterExpectedMemoryLeak;
UnregisterExpectedMemoryLeak: RegisterUnregisterExpectedMemoryLeak
);

initialization
SetMemoryManager(MemoryManager);

end.

将其列为 .dpr 文件的 use 子句中的第一个单元。完成此操作后,WinDbg !heap 应该开始查看您的 Delphi 堆分配。

关于delphi - 为什么 Windbg 无法看到 Delphi 中创建的内存泄漏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40807516/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com