本文介绍了当内存分配号不总是相同时,如何检测内存泄漏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的程序出现了这样的内存泄漏(55字节).我正在使用C ++,MFC,Visual Studio 2010.

I got a memory leak (55 bytes) on my program like this.I am using C++, MFC, Visual Studio 2010.

检测到内存泄漏!{13497}正常块,位于0x0E44C248,长55个字节.数据:44 3A 5C 46 44 41 53 ....

Detected memory leaks!{13497} normal block at 0x0E44C248, 55 bytes long.Data: 44 3A 5C 46 44 41 53....

问题是,内存分配号"13497"并不总是相同.如果我再次运行该程序,它将始终是不同的数字.我想使用_crtBreakAlloc查找退出前未释放内存的位置,但似乎无法中断内存分配号.我也使用了_CrtSetDbgFlag和_CrtDumpMemoryLeaks,但效果也不佳.

the problem is, the memory allocation number "13497" is not always same. it's always different number if I run the program again.I wanted to find where I didn't release the memory before the exit, with _crtBreakAlloc, but it seems not possible to break on a memory allocation number.I used _CrtSetDbgFlag, and _CrtDumpMemoryLeaks as well, but it also didn't work well.

在这种情况下,有什么方法可以检测到内存泄漏吗?

Is there any way to detect the memory leak in this case?

谢谢.

推荐答案

您可以使用cppcheck之类的静态分析器,或者如Joe所说,使用重映射运算符new.

You can use a static analizer like cppcheck or, as Joe said, remap operator new.

我开发了一些可以使用的内存泄漏实用程序:

I developed some memory leaks utilities that you can use:

https://github.com/check69/Utils/blob/master /leaks.cpp

https://github.com/check69/Utils/blob/master /leaks.h

有一些Visual Studio指令可以获取控制台输出中的漏洞,以便更轻松地调试.

There is some visual studio instruction to get the leaks in the console output, to debug easier.

PS:我会将其作为评论发布在joe帖子中,但我需要50分才能发表评论.

PS: I would put this as a comment in joe post, but I need 50 points to put comments.

这篇关于当内存分配号不总是相同时,如何检测内存泄漏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 09:03