问题描述
我有一个现在很大的MFC SDI项目.我需要使用Visual Studio检查内存泄漏.我已经阅读了许多文章中推荐的代码.
因此,在我要检查的源文件的顶部,我添加了:
I have an MFC SDI project that is quite big now. I need to check for memory leaks using Visual Studio. I have put in the code recommended in lots of articles I have read.
So, at the top of the source file that i want to check, I have added:
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
因为我想在报告泄漏的行上获取一些信息,这将使我能够在源代码中找到发生泄漏的点,所以我已经提出:
as I want to get some info on the line that reports the leak that will allow me to find the point in the source code where he leak occurs I have put:
#ifdef _DEBUG
#ifndef DBG_NEW
#define DBG_NEW new ( _NORMAL_BLOCK , __FILE__ , __LINE__ )
#define new DBG_NEW
#endif
#endif">
现在,当我进行编译时,在链接阶段我得到:
myfile.obj LNK2005:"void __cdecl运算符删除(void * .... etc.
已在uafxcwd.lib(afxmem.obj)中定义
myapp.exe:致命错误LNK 1169:已定义一个或多个乘法定义的符号.
有人可以告诉我如何在MFC项目中查找内存泄漏-似乎已经定义的内容与我要添加的内容之间存在冲突.我已经阅读了所有可以找到的内容,但还没有更明智(并且很沮丧:().
now when I compile, at the linking stage I get:
myfile.obj LNK2005: "void __cdecl operator delet(void* ....etc.
already defined in uafxcwd.lib (afxmem.obj)
myapp.exe: fatal error LNK 1169: one or more multiply defined symbols defined.
Could somebody please tell me how to find memory leaks in an MFC project - it seems like there is a conflict bteween things that are already defined and what I am trying to add. I have read all that I can find but am no wiser yet (and am very frustrated :( ).
推荐答案
#include <string.h>
#include <malloc.h>
#include <crtdbg.h>
int main( )
{
char *p1, *p2;
int tmpDbgFlag;
_CrtSetReportMode( _CRT_ERROR, _CRTDBG_MODE_FILE );
_CrtSetReportFile( _CRT_ERROR, _CRTDBG_FILE_STDERR );
/*
* Set the debug-heap flag to keep freed blocks in the
* heap's linked list - This will allow us to catch any
* inadvertent use of freed memory
*/
tmpDbgFlag = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG);
tmpDbgFlag |= _CRTDBG_DELAY_FREE_MEM_DF;
tmpDbgFlag |= _CRTDBG_LEAK_CHECK_DF;
_CrtSetDbgFlag(tmpDbgFlag);
.....
}
此外,您将有助于查看以下链接:
http://msdn.microsoft.com/en-us/library/1y71x448%28v = vs.71%29.aspx [ ^ ]
http://mariusbancila.ro/blog/2010 /07/12/how-to-find-the-source-of-memory-leaks/ [ ^ ]
内存泄漏检测 [ ^ ]
http://msdn.microsoft.com/en-us/library/c99kz476%28v = vs.80%29.aspx [ ^ ]
http://msdn.microsoft.com/en-us/library/x98tx3cf%28v = vs.71%29.aspx [ ^ ]
In addition you will be helpful to review the following links:
http://msdn.microsoft.com/en-us/library/1y71x448%28v=vs.71%29.aspx[^]
http://mariusbancila.ro/blog/2010/07/12/how-to-find-the-source-of-memory-leaks/[^]
Memory Leak Detection[^]
http://msdn.microsoft.com/en-us/library/c99kz476%28v=vs.80%29.aspx[^]
http://msdn.microsoft.com/en-us/library/x98tx3cf%28v=vs.71%29.aspx[^]
这篇关于在MFC项目中发现内存泄漏的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!