Search code examples
c++cvisual-studio-2010debuggingcrt

CrtDebug Stackoverflow?


I'm getting an access violation I can't quite debug.

I seem to be getting some kind of recursive error inside of the CRT call _CrtCheckMemory.

The following is the call stack (bottom -> top), where the ... is just removed repeated messages.

msvcr100d.dll!__chkstk()    Unknown
msvcr100d.dll!_VCrtDbgReportA(int nRptType, const char * szFile, int nLine, const char * szModule, const char * szFormat, char * arglist) Line 252  C
msvcr100d.dll!_CrtDbgReportV(int nRptType, const char * szFile, int nLine, const char * szModule, const char * szFormat, char * arglist) Line 242   C
msvcr100d.dll!_CrtDbgReport(int nRptType, const char * szFile, int nLine, const char * szModule, const char * szFormat, ...) Line 258   C
msvcr100d.dll!_CrtCheckMemory() Line 1817   C++
msvcr100d.dll!_heap_alloc_dbg_impl(unsigned __int64 nSize, int nBlockUse, const char * szFileName, int nLine, int * errno_tmp) Line 383 C++
msvcr100d.dll!_nh_malloc_dbg_impl(unsigned __int64 nSize, int nhFlag, int nBlockUse, const char * szFileName, int nLine, int * errno_tmp) Line 239  C++
msvcr100d.dll!_calloc_dbg_impl(unsigned __int64 nNum, unsigned __int64 nSize, int nBlockUse, const char * szFileName, int nLine, int * errno_tmp) Line 601  C++
msvcr100d.dll!_getptd_noexit() Line 470 C
msvcr100d.dll!_XcptFilter(unsigned long xcptnum, _EXCEPTION_POINTERS * pxcptinfoptrs) Line 202  C
msvcr100d.dll!_callthreadstartex$filt$0() Line 316  C
msvcr100d.dll!__C_specific_handler(_EXCEPTION_RECORD * ExceptionRecord, void * EstablisherFrame, _CONTEXT * ContextRecord, _DISPATCHER_CONTEXT * DispatcherContext) C
ntdll.dll!0000000077989d0d()    Unknown
ntdll.dll!00000000779791af()    Unknown
ntdll.dll!00000000779b1278()    Unknown
msvcr100d.dll!__chkstk()    Unknown
...
msvcr100d.dll!_VCrtDbgReportA(int nRptType, const char * szFile, int nLine, const char * szModule, const char * szFormat, char * arglist) Line 298  C
msvcr100d.dll!_CrtDbgReportV(int nRptType, const char * szFile, int nLine, const char * szModule, const char * szFormat, char * arglist) Line 242   C
msvcr100d.dll!_CrtDbgReport(int nRptType, const char * szFile, int nLine, const char * szModule, const char * szFormat, ...) Line 258   C
msvcr100d.dll!_CrtCheckMemory() Line 1817   C++
msvcr100d.dll!_heap_alloc_dbg_impl(unsigned __int64 nSize, int nBlockUse, const char * szFileName, int nLine, int * errno_tmp) Line 383 C++
msvcr100d.dll!_nh_malloc_dbg_impl(unsigned __int64 nSize, int nhFlag, int nBlockUse, const char * szFileName, int nLine, int * errno_tmp) Line 239  C++
msvcr100d.dll!_calloc_dbg_impl(unsigned __int64 nNum, unsigned __int64 nSize, int nBlockUse, const char * szFileName, int nLine, int * errno_tmp) Line 601  C++
msvcr100d.dll!_getptd_noexit() Line 470 C
msvcr100d.dll!_errno() Line 280 C
msvcr100d.dll!_VCrtDbgReportA(int nRptType, const char * szFile, int nLine, const char * szModule, const char * szFormat, char * arglist) Line 298  C
msvcr100d.dll!_CrtDbgReportV(int nRptType, const char * szFile, int nLine, const char * szModule, const char * szFormat, char * arglist) Line 242   C
msvcr100d.dll!_CrtDbgReport(int nRptType, const char * szFile, int nLine, const char * szModule, const char * szFormat, ...) Line 258   C
msvcr100d.dll!_CrtCheckMemory() Line 1817   C++
msvcr100d.dll!_free_dbg_nolock(void * pUserData, int nBlockUse) Line 1288   C++
msvcr100d.dll!_free_dbg(void * pUserData, int nBlockUse) Line 1265  C++
msvcr100d.dll!_freefls(void * data) Line 622    C
msvcr100d.dll!_freeptd(_tiddata * ptd) Line 683 C
msvcr100d.dll!_endthreadex(unsigned int retcode) Line 365   C
msvcr100d.dll!_callthreadstartex() Line 315 C
msvcr100d.dll!_threadstartex(void * ptd) Line 297   C
kernel32.dll!000000007729652d() Unknown
ntdll.dll!000000007798c521()    Unknown

Any idea what might be causing this or advice on how I might go about debugging?


Solution

  • Well, that's a bit tragic. It is trying to generate a diagnostic to tell you that the heap is corrupted. The reporting code tries to obtain the value of errno in order to display it, but that's a static CRT variable that gets allocated on demand. So it allocates memory. Which dies, the heap is corrupted. Which triggers a diagnostic to tell you that the heap is corrupted. You can guess the rest, this goes on and on until it runs out of stack.

    One workaround is to add this bit of code to the start of main (or the thread):

     int dummy = errno;
    

    so the CRT allocates memory before the heap gets corrupted. You'll still have to find the cause of the corruption.