1

I ran into a really nasty linker error when I start using the debugging discussed here.

I managed to narrow it down to the new ( _NORMAL_BLOCK , __FILE__ , __LINE__ ) construct. As soon as this overload of the new operator was used I get error LNK2005: "void __cdecl operator delete(void *,int,char const *,int)" (??3@YAXPEAXHPEBDH@Z) already defined in ...

If I exclude that object file from the linking process it just points me to another. I haven't verified this but I'm quite sure they all of these object files that clash with MFC use the debug new version of the operator.

This lead me on a wild goose chase because if you Google this error, all the evidence will tell you that you're mixing CRT and MFC stuff in the wrong order and sure enough, this is what I was doing.

I went trough all my object files and libraries with dumpbin /directives and made sure that the order of linking was the right one, despite of all this I never got past that error.

Is the CRT debugging technique inherently incompatible with MFC or is there an explanation for this?

I'm not very knowledgeable about MFC and I'm trying to move away from MFC entirely but I'd like to get this to work until that happens.

John Leidegren
  • 59,920
  • 20
  • 131
  • 152
  • I tried to use CRT stuffs twice for detecting memory leaks with no success. I would recommend your own wrapping or detours. Or use one of the memory management tools. – Kirill Kobelev Sep 02 '12 at 10:41
  • Could you post the entire linker error message? – john Sep 02 '12 at 10:42
  • @john Sure... if it helps. I updated the question to include the full error, I've only stripped to object and file name. – John Leidegren Sep 02 '12 at 11:00
  • @john, Have you tried the same set of CRT functions on a small sample? – Kirill Kobelev Sep 02 '12 at 11:04
  • @KirillKobelev No, I ended up spending a whole day on that, after which I moved the entire project from VC2008 to VC2010 with no success. Atm, I can't spend more time debugging this, I'm really hoping for someone more knowledgeable and experienced to tell me what's up with the linker process... – John Leidegren Sep 02 '12 at 11:31
  • @KirillKobelev as things stand right now, I'm using the block IDs from the debug heap to break using the `_CrtSetBreakAlloc` function and it's more time consuming but get's the job done. – John Leidegren Sep 02 '12 at 11:32
  • Have you tried this -http://support.microsoft.com/kb/148652? And check this thread http://stackoverflow.com/questions/1146338/error-lnk2005-new-and-delete-already-defined-in-libcmtd-libnew-obj. – SChepurin Sep 02 '12 at 11:43
  • @SChepurin Yeah, like I said in my question, you would think that this is the issue but it is not. – John Leidegren Sep 04 '12 at 06:59

1 Answers1

1

MFC applications will detect memory leaks for you when in debug mode. To get more information about the leak, you can define new to DEBUG_NEW at the top of each file as follows:

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

For more information, see the MSDN article Memory Leak Detection in MFC

TheSteve
  • 1,138
  • 6
  • 12