/MT and /MD builds crashing, but only when debugger isn't attached: how to debug?

This question already has an answer here:

  • Program only crashes as release build — how to debug? 26 answers

  • One little know difference between running with debugger attached or not is the OS Debug Heap (see also Why does my code run slowly when I have debugger attached?). You can turn the debug heap off by using environment variable _NO_DEBUG_HEAP . You can specify this either in your computer properties, or in the Project Settings in Visual Studio.

    Once you turn the debug heap off, you should see the same crash even with debugger attached.

    That said, be aware memory corruptions can be hard to debug, as often the real cause of the corruption (like some buffer overrun) may be very far from where you see the symptoms (the crash).


    Crashing inside new or malloc usually is a hint that the (internal) structure of the malloc implementation has been corrupted. This is most of the time done by writing past a previous allocation (buffer overflow). Then on the next call to new or malloc the app crashes as the internal structure now contains invalid data.

    Check if you may overwrite any previous allocated space.

    If your application is portable you may try to build it on Linux and run it under Valgrind.


    Application Verifier was super-useful for solving this once I had _NO_DEBUG_HEAP=1 in environment, see the accepted answer here: Finding where memory was last freed?

    It's probably also worth mentioning pageheap, which I found while looking at Application Verifier. Looks like it covers some similar ground.

    (FYI, it was a one-character buffer overflow:

    m_pEnumName = (char*)malloc(strlen(data) /* missing +1 here */);
    strcpy(m_pEnumName, data);
    

    ...yet another ridiculously good argument to not use strcpy directly.)

    链接地址: http://www.djcxy.com/p/14440.html

    上一篇: 释放分配在不同DLL中的内存

    下一篇: / MT和/ MD会造成崩溃,但只有当调试器未连接时:如何调试?