Click here to Skip to main content
15,888,177 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
In my programs in Visual C++, sometimes when I write new code, build and run,it doesn't work. But when I add break points, run in debugger , it gives the right answers.

Because just couple of minutes back I was showing my work to my manager, where in I was asked to put new values for testing n it never worked n later after he left, I tested in debugger n it worked!!

I'm experiencing this kind of behaviour for the fifth time .Does anyone know why ?
Posted
Updated 8-May-12 4:34am

Most commonly, your problem of Debug build running correctly and Release build not running, is due to using constructs such as:

ASSERT (a = func());

In release version "func" is not called - in fact the whole ASSERT statement is substituted with nothingness.

Execution speed unless you have a very time-sensitive real-time application is NOT likely at all.
 
Share this answer
 
v2
Comments
[no name] 9-May-12 1:40am    
"Execution speed unless you have a very time-sensitive real-time application is NOT likely at all."
This conclusion is not correct. Any RT programmer knows this. Timing is comletely different in debug vs release versions.

http://preshing.com/20110717/the-windows-heap-is-slow-when-launched-from-the-debugger
michaelmel 9-May-12 6:10am    
Have you even read the question and my answer?

I am saying it is NOT important UNLESS it is a R/T application.

From the question it doesn't sound at all as a R/T app.
ARopo 10-May-12 6:36am    
Solution 3 could indeed be the case, there are many reasons why debug and release can give different outcomes, I've certainly been tripped up by putting vital code into an assert macro.
Is it that you break point is holding things up long enough for some precondition to happen?

Is it that it work when you switch active windows to the debbuger?

Are you sure your running the same binaries each time? in this situation I sometimes delete all occurances of my dlls/exe then rebuild and run

If you are running release or debug Could this be the reason:

One common problem is when you build release you can get a different value in variables that are not initialised e.g. the following will behave differently
C++
BOOL myBool

if (myBool)
   // this will be the path in release
else
   // this will be the path in debug

void* pMyPointer

if (pMyPointer)
   // this will be the path in release
else
   // this will be the path in debug

The reason for this is that the debugger zeros memory, in release mode the memory contains previous data this is why it is important to inialise varables

e.g.
C++
BOOL myBool = FALSE;
pMyPointer = nullptr;
 
Share this answer
 
v2
Comments
Aescleal 8-May-12 10:46am    
Good point about different speeds of code - debug code usually moves slower than release code so it might expose a threading threading issue like reading a pointer before it's written by another thread.

Ash
Depending on what version of VC++ you're using the debugger might initialise memory differently from when you run it from the shell. So you might be reading/writing somewhere you shouldn't which crashes the release mode version but due to whatever the debuggers throwing in there it happens to be valid...

As a quick set of tips:

- Don't use the debugger 95% of the time, use unit tests to verify your code and run them every time you build
- Fork out for a copy of PClint and use it to check that you're not doing something naughty in your code - like dereferencing an uninitialised pointer
- Use the latest compiler you can get, newer compilers are better at warning about dangerous things

Cheers,

Ash
 
Share this answer
 
these used for debug in vc++ ctrl+shift+b
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900