C ++ on Windows: function to get allocated memory?

I am coding in C ++ using Visual Studio 2008 on Windows 7.

My application has a memory leak, I see it using the system monitor.

I need to open it in code.

Is there a function that returns the amount of memory allocated to the calling process?

+3
source share
1 answer

There is a specific MSVC solution for detecting memleak

// enable memory leaks detection
#if !defined(NDEBUG)
HANDLE hLogFile = CreateFile( "log.txt", GENERIC_WRITE, FILE_SHARE_WRITE,
                              NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL );
#endif

_CrtSetReportMode( _CRT_ASSERT, _CRTDBG_MODE_FILE | _CRTDBG_MODE_WNDW  | _CRTDBG_MODE_DEBUG );
_CrtSetReportMode( _CRT_WARN, _CRTDBG_MODE_FILE   | _CRTDBG_MODE_DEBUG );
_CrtSetReportMode( _CRT_ERROR, _CRTDBG_MODE_FILE  | _CRTDBG_MODE_WNDW  | _CRTDBG_MODE_DEBUG );

_CrtSetReportFile( _CRT_ASSERT, hLogFile );
_CrtSetReportFile( _CRT_WARN,   hLogFile );
_CrtSetReportFile( _CRT_ERROR,  hLogFile );

int tmpDbgFlag = _CrtSetDbgFlag( _CRTDBG_REPORT_FLAG );
tmpDbgFlag |= _CRTDBG_ALLOC_MEM_DF;
tmpDbgFlag |= _CRTDBG_DELAY_FREE_MEM_DF;
tmpDbgFlag |= _CRTDBG_LEAK_CHECK_DF;
_CrtSetDbgFlag( tmpDbgFlag );

if ( BlockIndex > 0 )
{
    _CrtSetBreakAlloc( BlockIndex );
}

This creepy code includes the file protocol of all unallocated blocks. Of course, it is deeply connected with the debug version of MSVCRT

+4
source

All Articles