Click here to Skip to main content
15,885,141 members
Please Sign up or sign in to vote.
4.33/5 (3 votes)
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">

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 :( ).
Posted
Comments
Sergey Alexandrovich Kryukov 3-Aug-12 14:01pm    
How can you talk about a leak, if you still have a problem to link it? :-)
--SA
Jackie Lloyd 6-Aug-12 9:19am    
I put some leaks in to test if they were being shown up - before I put in the code which wouldn't link.
Volynsky Alex 9-Aug-12 9:03am    
A very useful question. +5!

1 solution

You can use something like :
C++
#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);
.....
}

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[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900