检测 C++/windows 中的内存泄漏

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3482854/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 15:02:26  来源:igfitidea点击:

detecting memory leaks in C++ / windows

c++windowscommemory-leaks

提问by sbi

For debugging purposes, when I'm writing an app, the first thing I do is put the following into the stdafx.h:

出于调试目的,当我编写应用程序时,我做的第一件事是将以下内容放入 stdafx.h:

// -- leak detection ----------------------------------------------------------
#ifdef _DEBUG   
// http://msdn.microsoft.com/en-us/library/e5ewb1h3(v=VS.80).aspx
#define _CRTDBG_MAP_ALLOC   
#include <stdlib.h>
#include <crtdbg.h>
#define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__)
#define new DEBUG_NEW
#endif

Then I add the following to the beginning of the program's main() function:

然后我将以下内容添加到程序的 main() 函数的开头:

#ifdef _DEBUG
_CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
//_CrtSetBreakAlloc( 670 );
#endif  

Redefining the new operator to give leak information is a useful tool. But what about CoTaskMemAlloc and CoTaskMemFree ? How can I detect leaks using these?

重新定义新运算符以提供泄漏信息是一个有用的工具。但是 CoTaskMemAlloc 和 CoTaskMemFree 呢?如何使用这些检测泄漏?

I'm writing software that uses COM and DirectShow and need to know how to trace leaks caused by using CoTask allocations.

我正在编写使用 COM 和 DirectShow 的软件,需要知道如何跟踪使用 CoTask 分配引起的泄漏。

thanks!

谢谢!

采纳答案by celavek

Visual Leak Detector- pretty easy to use and there'e no overhead for the app built in release.

Visual Leak Detector- 非常易于使用,内置版本的应用程序没有开销。

回答by sbi

Get rid of manual memory management and you'll get rid of leaks. Embrace RAII, and never use a resource unless it's wrapped in a handler whose single purpose is to wrap that resource.

摆脱手动内存管理,您将摆脱泄漏。拥抱 RAII,永远不要使用资源,除非它被包装在一个处理程序中,该处理程序的唯一目的是包装该资源。

I don't think I had a memory leak (or a crash, FTM) in years. But then I have written deleteless than half a dozen times in the last decade.

我认为我多年来没有发生内存泄漏(或崩溃,FTM)。但是delete在过去的十年里,我写了不到六次。

回答by obelix

There is also application verifier. It can track a whole bunch of other issues as well apart from leaks like places where you forget to free win32 objects such as handles etc ...

还有应用程序验证器。除了泄漏之外,它还可以跟踪一大堆其他问题,例如您忘记释放 Win32 对象(例如句柄等)的地方......

The MSDN link is: http://msdn.microsoft.com/en-us/library/ms220948(VS.80).aspx

MSDN 链接是:http: //msdn.microsoft.com/en-us/library/ms220948(VS.80).aspx

Taken from a similar quesiton at Visual C++ - Memory Leak Detection

取自Visual C++的类似问题- 内存泄漏检测

回答by dirkgently

But what about CoTaskMemAlloc and CoTaskMemFree ? How can I detect leaks using these?

但是 CoTaskMemAlloc 和 CoTaskMemFree 呢?如何使用这些检测泄漏?

You cannot for the same reason that malloc/freedoesn't help you to detect leaks. You need to wrap them suitably to help you with leak detection.

出于同样的原因,您不能malloc/free不能帮助您检测泄漏。您需要适当地包裹它们以帮助您进行泄漏检测。

As other posters have said if you are worried about leaks, then start designing your application ground up with this requirement in mind. Use custom allocators where you can manage/track allocations/deallocations.

正如其他发帖人所说,如果您担心泄漏,那么开始设计您的应用程序时要牢记这一要求。使用自定义分配器,您可以在其中管理/跟踪分配/解除分配。

Did you happen to visit this question: Usage of CoTaskMemAlloc?

您是否碰巧访问过这个问题:Usage of CoTaskMemAlloc?