1

Apologies for poor formatting- writing this on my phone.

I am getting many false memory leaks reported when exiting my MFC (also cli) app.

On investigation, MFC AfxDiagnosticInit function is being called too early, and consequently the memory leak dump is occuring before my CWinApp destructor is being called, resulting in MANY false positive memory leaks

How can I ensure AfxDiagnosticInit is called sooner, so that my static variable destructor (e.g. CMyApp) is called before the leak dump starts?

Oleksi
  • 12,947
  • 4
  • 56
  • 80
Cechner
  • 849
  • 7
  • 19
  • Does windows have leak detection for `new`/`delete`? It may only work properly with `malloc()`/`free()` (to my experience). But, naturally, `malloc`/`free` does not belong in C++ code. – RageD Apr 17 '12 at 03:10
  • Seems to be expected: http://msdn.microsoft.com/en-us/library/x98tx3cf(v=vs.110).aspx (at the bottom - "False Positives") – ta.speot.is Apr 17 '12 at 03:10

3 Answers3

1

Just a follow up: I found the real problem, it was relating to the fact that it is a mixed-mode C++/CLI application, and the CLI wasn't allowing the CRT to shut down.

Revised question here: Mixed-mode C++/CLI app not shutting down CLR correctly

Community
  • 1
  • 1
Cechner
  • 849
  • 7
  • 19
0

Try finding WinMain(), the same function where CWinApp is constructed, and placing AfxDiagnosticInit() there.

Dan Nissenbaum
  • 13,558
  • 21
  • 105
  • 181
0

As an alternative solution, you can also disable automatic memory leak dumping by MFC using:

AfxEnableMemoryLeakDump(FALSE);

This is supported since Visual Studio 2010. For the documentation, see here.

You can dump the leaks at a more suitable location with _CrtDumpMemoryLeaks();

helb
  • 7,609
  • 8
  • 36
  • 58