There is a specific MSVC solution for detecting memleak
#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
source
share