Durnham
Durnham

Reputation: 21

C++ / VS 2010: Bug(s) occur only when running without a debugger

This is a twisted problem. When I run my program using F5 in Visual Studio, everything works fine. If I start it without the debugger, or from outside VS, a few nasty bugs which I can't locate occur.

I suspect this is caused by the debugger randomizing all uninitialized variables, wheras "outside", they are set to 0. I must be using a variable without initing it somewhere...

Upvotes: 2

Views: 552

Answers (2)

user1183961
user1183961

Reputation: 11

I had a similar problem recently except it was even weirder. The code worked fine when I ran release in visual studio, but when I ran the program outside visual studio (just clicked the .exe) it would do this very big bug.

Turned out it was because of:

angle = MathHelper.ToRadians(angle);

When ever angle was 0 it would fail and produce some weird results. I fixed it by simply changing it to:

angle = MathHelper.ToRadians(angle+.01f);

Very very annoying problem for something so small. Hopefully this helps others find similar errors.

Upvotes: 0

Peter Alexander
Peter Alexander

Reputation: 54300

As Hans Passant says, you have it the wrong way round. In Debug, memory is initialised, but in release it could be anything.

In general, if you have something going wrong in release that doesn't happen in debug then it could be down to a few things:

  1. Relying on uninitialised variables as you said.
  2. Optimisations changing the semantics of your code. This will only happen if you write code that is relying on ill-defined behaviour. For example, assuming that function arguments are evaluated in a specific order, or relying on signed integer overflow, or any number of things.
  3. It's a timing issue that shows up more often in release builds due to the better performance. These most often occur in multithreaded applications.
  4. You use different libraries in debug and release and rely on the different behaviour between them.

You can use the debugger to attach to a running program. I think it's in the 'Debug' menu in VS and is called 'Attach to process...'. Make sure that you generate debug symbols for release builds so that you get a usable call stack.

Upvotes: 4

Related Questions