>>I could then try and find any creeping memory leaks this way.
If your goal is to find memory leaks, that is a way, but there are better ones. E.g.
#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 number where the allocation occured.
Main Topics
Browse All Topics





by: ZoppoPosted on 2009-05-05 at 09:54:12ID: 24306688
Hi Wanting2LearnMan,
'GlobalMemoryStatus' can be used to gather information about system-wide memory information.
To find information about your program's memory usage you can use 'GetProcessMemoryInfo' from PSAPI, i.e.:
> #include <psapi.h>
> #pragma comment( lib, "psapi.lib" )
> ...
> HANDLE hProcess = GetCurrentProcess();
> PROCESS_MEMORY_COUNTERS info = { sizeof( info ) };
> GetProcessMemoryInfo( hProcess, &info, sizeof( info ) );
>
> int *x = new int [ 200000 ];
>
> GetProcessMemoryInfo( hProcess, &info, sizeof( info ) );
>
> delete [] x;
>
> GetProcessMemoryInfo( hProcess, &info, sizeof( info ) );
Hope that helps,
ZOPPO