Visual Studio 2008(C ++)内存泄漏检测不显示文件/方法位置 - 如何让它工作? [英] Visual Studio 2008 (C++) memory leak detection not showing file/method location - how to get that to work?

查看:405
本文介绍了Visual Studio 2008(C ++)内存泄漏检测不显示文件/方法位置 - 如何让它工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用此处的说明尝试查找Win32应用程序中的内存泄漏。如上所述,我把

  #define _CRTDBG_MAP_ALLOC 
#include< stdlib.h>
#include< crtdbg.h>

在文件顶部的行(包含WINAPI _tWinMain的cpp文件),然后在退出我添加了winmain的点

  _CrtDumpMemoryLeaks(); 

不幸的是,我没有看到泄漏的行号/位置



我也尝试过

  _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF); 
_CrtSetReportMode(_CRT_ERROR,_CRTDBG_MODE_DEBUG);

在winmain开始 - 再次没有运气。



我发现这很奇怪,因为我通常没有找到泄漏或自动报告的问题。



这是一个巨大的,旧的遗留应用程序我正在为一个新雇主工作。在过去,我已经从标准的VS向导工作。



有关如何获取源行/方法,导致泄漏的任何建议? (或至少是新电话的行?



EDIT:



我也尝试过视觉检漏仪 - 没有成功。



非常很奇怪。



EDIT



我尝试使用下面列出的redefinition of new,

$ p

解决方案

您确定泄漏的代码是使用CRT调试分配例程吗?这需要使用 malloc() new (相对于 LocalAlloc GlobalAlloc ,一些自定义块分配器等)和 _DEBUG (我认为)必须定义时, 。



为了获取泄漏的源代码行,您需要定义 DEBUG_NEW 。这是因为为了跟踪它们,每个分配必须被包括 __ FILE __ __ LINE __ 的调用替换。从向导的标准定义看起来像:

  #ifdef _DEBUG 
#define _CRTDBG_MAP_ALLOC
#include < stdlib.h>
#include< crtdbg.h>
#define DEBUG_NEW new(_NORMAL_BLOCK,__FILE__,__LINE__)
#define new DEBUG_NEW
#endif

这不会处理 malloc ,可能有类似的咒语,如果你调试的代码使用 malloc 而不是 new



如果您使用预编译的标题,你可以把它放在预编译的头文件中,它将影响该项目中的所有源文件。


I am using the instructions found here to try to find memory leaks in a Win32 application. As described, I put the

#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>

Lines at the top of a file (the cpp file that contains WINAPI _tWinMain) and then at the exit point of winmain I added

_CrtDumpMemoryLeaks();

Unfortunately I do not see the line numbers/locations for the leaks (but I do get a list of leaks).

I also tried putting

_CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF); 
_CrtSetReportMode ( _CRT_ERROR, _CRTDBG_MODE_DEBUG);

at the beginning of winmain - and again, no luck.

I find this odd because I usually have had no problems ever finding leaks or having them reported automatically.

This is a huge, old legacy app I am working on for a new employer. In the past I have worked from the standard VS wizard.

Any suggestions on how to get source lines/methods that are causing the leaks? (or at least the lines for the "new" calls?

EDIT:

I also tried visual leak detector - with no success.

Very strange.

EDIT

I tried using the redefinition of new as listed below, however I get errors when boost is compiled in.

解决方案

Are you sure the code that's leaking is using the CRT debug allocation routines? That requires using malloc() or new (as opposed to LocalAlloc, GlobalAlloc, some custom block allocator, etc..) and that _DEBUG (I think) must be defined when the CRT headers were included.

In order to get source lines for leaks, you will need to define DEBUG_NEW everywhere the allocations occur. This is because in order to track them, each allocation must be replaced with a call that includes __FILE__ and __LINE__. The standard definition from the wizard looks something like:

#ifdef _DEBUG
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
#define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__)
#define new DEBUG_NEW
#endif

This doesn't handle malloc, there's probably a similar incantation for that, if the code you're debugging uses malloc instead of new.

If you're using pre-compiled headers, you can just put this in the precompiled header file and it will affect all the source files in that project.

这篇关于Visual Studio 2008(C ++)内存泄漏检测不显示文件/方法位置 - 如何让它工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆