Ok, I can add a new header file in the solution. But then does that really need to be included in EACH cpp file I have in the Solution? It's tooo big and has many different projects inside. Some have stdafx (in that condition I'd add your new header file at the end of stdafx.h) and some don't have...
Main Topics
Browse All Topics





by: jkrPosted on 2009-02-09 at 09:54:42ID: 23592431
You can get the line numbr and file information from the C runtime directly, e.g.:
LE__,__LIN E__); ame,__line );
Add the following to a central header file:
#ifdef _DEBUG
#ifndef _DBG_NEW
#include <crtdbg.h>
inline void* __operator_new(size_t __n) {
return ::operator new(__n,_NORMAL_BLOCK,__FI
}
inline void* _cdecl operator new(size_t __n,const char* __fname,int __line) {
return ::operator new(__n,_NORMAL_BLOCK,__fn
}
inline void _cdecl operator delete(void* __p,const char*,int) {
::operator delete(__p);
}
#define _DBG_NEW new(__FILE__,__LINE__)
#define new _DBG_NEW
#endif // _DBG_NEW
#else
#define __operator_new(__n) operator new(__n)
#endif
Then, add
int tmpFlag;
// Get the current state of the flag
// and store it in a temporary variable
tmpFlag = _CrtSetDbgFlag( _CRTDBG_REPORT_FLAG );
// Turn On (OR) - Keep freed memory blocks in the
// heap’s linked list and mark them as freed
tmpFlag |= _CRTDBG_LEAK_CHECK_DF;
// Set the new state for the flag
_CrtSetDbgFlag( tmpFlag );
to your app's start code and call
_CrtDumpMemoryLeaks ();
to before the program ends and you'll get the line numbers where the allocations occured.
(Ref.: http:Q_21009673.html)