Search code examples
c++debuggingmemory-leaksheap-memorycorruption

Is it okay if program has Memory corruption while debugging, but runs fine in relase


I have a program which causes Memory corruption while debugging, but runs perfectly fine in release. To add strangeness, program fulfils its function before "crashing" (it saves data to a file). Can that mean Visual Studio simply doesn't like my program, or does it mean I have a serious bug I need to repair? In general, if Debug mode crashes, does it always mean there is problem with a program, or is it possible there is simply problem with the way program runs in debug mode, so I should not worry too much?


Solution

  • It is almost certainly a bug in your application, which will need tracking down and fixing.

    There are many types of bugs that lead to undefined behaviour. Some types of undefined behaviour (such as memory corruption) can manifest themselves as seemingly random failures that occur much later in the program than the bug that's caused them.

    In debug mode, Visual Studio goes out of its way to ensure that such failures occur as early as possible and are as prominent as possible. In release mode, the focus is on performance.

    There is a good summary of some of the differences between debug and release in https://stackoverflow.com/a/312352/367273