制作UMDH感比较输出UMDH

2023-09-08 01:10:10 作者:心碎葬海

我已经建立了一个测试工具来测试从DLL我开发了一个项目组功能。

I have set up a testing harness to test a set of functions from a DLL I am developing for a project.

我想验证功能有零内存泄漏在Windows系统上。

I want to verify that the functions have zero memory leaks on a windows system.

我已经正确地设置了UMDH,我能够得到的两个堆转储一个比较文件。即使是堆栈跟踪都显示了罚款。

I have properly set up UMDH and I am able to get a compare file between two Heap dumps. Even the stack traces are showing up fine.

从差异输出困扰我,我希望有人能帮助解释为什么我收到我得到的输出。

The output from the diff puzzles me and I was hoping someone could help explain why I am getting the output I am getting.

+      56 (     56 -      0)      1 allocs  BackTrace9C160
+       1 (      1 -      0)    BackTrace9C160  allocations

我成立了LOG1和LOG2是相隔1整数分配,只是为了验证我的设置。

I set up the log1 and log2 to be 1 integer allocation apart, just to verify my setup.

实际上,仅显示1分配,但是,它是说,存在来自之前和之后56个字节的变化。我只会想到的sizeof(int)的以字节为单位的变化。在我的系统,sizeof的一个int分配为4个字节,所以我期待看到一个+4,而不是+56。

Indeed, it only shows 1 allocation, however, it is saying that there is a 56 byte change from the before and after. I would only expect sizeof(int) change in bytes. On my system, the sizeof an int allocation is 4 bytes, so I was expecting to see a +4, not a +56.

再次,code中的唯一行正在运行的日志之间是

Again, the only line of code being ran in between the logs is

new int; //purposely leak memory

任何解释?

IDE /编译器:Visual Studio 2010中

IDE/compiler: Visual Studio 2010

应用是64位

一个DLL参与(但我没有,即使在这个简单的INT分配例如打电话吧)

A DLL is involved (but I did not even call to it in this simple int allocation example)

如果我注释掉泄漏,我得到零分配和+ 0字节。所以我认为验证没有额外的字节数是从其他地方未来的应用程序,只需从上面显示,1号线...

If I comment out the leak, I get zero allocations and +0 bytes. So I think that verifies that no extra bytes are coming from anywhere else in the application, simply from that 1 line shown above...

请参见下面的SleuthEye的解决方案。而且,这里是一条评论我加入了评论的解决方案,我认为这是人谁最终会使用这个问题有利:

Please see SleuthEye's solution below. Also here is a comment I added as a comment to the solution that I believe is beneficial to people who end up using this question:

此外,如果你在你的.exe文件的发布版本中运行这个程序,还包括在运行目录下的程序调试数据库,UMDH将拉动内存泄漏的源文件名和行号,同时保持一个准确的字节计数。这给你调试的好处,并发布版本,只要内存泄漏的搜索去。

Additionally, if you run this program on a release build of your .exe and also include the program debug database in the running directory, umdh will pull the source file names and the line numbers of the memory leaks while maintaining an accurate byte count. This gives you the benefits of the debug and release builds as far as memory leak searching goes.

推荐答案

56字节使用调试堆时所描述上的 MSDN 。

The 56 bytes result from the additional memory allocated by the C run-time library (CRT) when using the debug heap, as described on MSDN.

看着dbbint.h,其中 _CrtMemBlockHeader 结构定义为:

Looking at dbbint.h, where the _CrtMemBlockHeader structure is defined as:

#define nNoMansLandSize 4

typedef struct _CrtMemBlockHeader
{
        struct _CrtMemBlockHeader * pBlockHeaderNext;
        struct _CrtMemBlockHeader * pBlockHeaderPrev;
        char *                      szFileName;
        int                         nLine;
#ifdef _WIN64
        /* These items are reversed on Win64 to eliminate gaps in the struct
         * and ensure that sizeof(struct)%16 == 0, so 16-byte alignment is
         * maintained in the debug heap.
         */
        int                         nBlockUse;
        size_t                      nDataSize;
#else  /* _WIN64 */
        size_t                      nDataSize;
        int                         nBlockUse;
#endif  /* _WIN64 */
        long                        lRequest;
        unsigned char               gap[nNoMansLandSize];
        /* followed by:
         *  unsigned char           data[nDataSize];
         *  unsigned char           anotherGap[nNoMansLandSize];
         */
} _CrtMemBlockHeader;

这之后将分配给存储你的 INT 键,然后进行额外的4个字节NoMansLand缓冲区。因此,对于64位应用程序的总分配的内存测试箱单 INT 过来对的sizeof(_CrtMemBlockHeader)+的sizeof(INT) +4 = 48 + 4 + 4 = 56

which would be followed by the memory allocated for your int and further followed by an additional 4 bytes "NoMansLand" buffer. So, for a 64 bit application the total allocated memory for your test case of a single int comes up to sizeof(_CrtMemBlockHeader)+sizeof(int)+4 = 48+4+4 = 56.

请注意,相同的分析运行在一个发布版本(其中 _CrtMemBlockHeader 未分配)产生以下比较日志输出:

Note that the same analysis ran on a release build (where the _CrtMemBlockHeader is not allocated) produces the following compare log output:

+       4 (      4 -      0)      1 allocs  BackTrace2
+       1 (      1 -      0)    BackTrace2  allocations
相关推荐