Click here to Skip to main content
12,752,876 members (38,211 online)
Rate this:
Please Sign up or sign in to vote.
See more: C++ Visual-Studio
My application works fine in debug mode. But it crashes arbitrarily in release mode. Crash point shows that either they are crashing in malloc or free. Do I need to change any project settings or something else need to be done?
Awaiting prompt response.

Posted 31-Aug-12 3:35am
pasztorpisti 31-Aug-12 9:26am
The reason for malloc/free to crash is usually heap corruption. This means that you use a pointer to an already deleted object and modify the pointed memory area. This is a common mistake. The other common bug is using a chunk of allocated memory and copying there with memcpy/strcpy by writing past the end of the allocated piece of memory. Another one is writing to an allocated array (like int array) by overindexing. These are the most common bugs. The memory in your heap consists of sections of the following 3 types: free memory, allocated memory, memory that is used by malloc/free to administer the allocated and free areas. If you overwrite something in the administrative areas then malloc/free crashes easily. I would mention that heap correction bugs are the most difficult to find besides thread synchronization bugs.
pasztorpisti 31-Aug-12 15:49pm
Some help from M$:
There are some other memory corruption seeker tools too that replace your malloc and free functions and do some trickery to find out if you wrote past your blocks, for example electric fence on linux. Use google to find tools/methods to detect memory corruption problems in C++. It won't be easy to catch bugs if the code quality is low, good luck!
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

I don,t Think that u have to change setting. i thing u have to check your code .
check malloc or free statement in release mode.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

There maybe several reasons why a program doesnt work in Release but Debug mode. Most common from my experience are wrong versions of third-party libraries your application may depend on (e.g. linking debug libraries in release builds).

But as you described, your program fails at some memory management task (freeing memory). This is often the case because of some false memory management in your code. The fact that it works in Debug mode doesn't really prove that your code is error free, especially when dealing with allocating and freeing memory. This is because in Debug mode the memory is allocated differently. Lets say that the Debug mode is more forgiving because more memory is allocated then is actually needed (for debugging purpose). Therefore if you try to access some memory that should be illegal (because you didnt allocate it properly) it could still work in Debug mode because of the additionaly allocated memory.

I would suggest you to look at your code and see if there maybe false allocations (by logic). In your described case i think is very unlikely that your problem is related to Project/Compiler settings.

I can also suggest you this article on CP about this topic which may be of great help to you:
Debugging Release Mode Problems[^]

Also this one seems to be very helpfull:
Surviving the Release Version[^]
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

The number 1 reason a program crashes in release mode but not debug mode is due to using an initialized variable. Uninitialized variables can have very different values in each mode so your program will behave differently when it used one of these. And make no mistake, if you use an unitialized variable your program *is* misbehaving in both modes, it just may not crash because of it in debug.

Make sure you initialize any variable before you use it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

    Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.170217.1 | Last Updated 31 Aug 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100