views:

71

answers:

3

Has anyone ever been debugging in Visual Studio and it appears to execute different code then the code that is being stepped through? Sometimes it even steps into what appears to be white space in the file that is being stepped through but VS is executing something anyway?

I've had this happen several times, once someone had me clear something in the Application Data directory under the user directory. However I can't remember what that was and wondered if anyone had some ideas.

+1  A: 

If your code is being optimized (project properties, build, "optimize code"), then the code being run will not necessarily match the code in the IDE. This is usually caused by trying to debug a release build. However, there are certain cases where this isn't the case.

FryGuy
+2  A: 

There are four most possible reasons.

Reason 1: you actually started not the compilation result but some other file. Path to the executable to start is set on the "Debugging" page of the project properties.

Reason 2: you have optimization on. In this case the compiler only saves line of code - to compiled code information for some code and not for other. You'll see that after you press "step over" you actually jump several statements ahead or that you can't access some variables values. Either turn off optimization or switch from interactive debugging to logging.

Reason 3: the program database (the .pdb file storing bindings of lines of code to compiled code) is out of sync with the compiled file. "Rebuild" project usually helps.

Reason 4: the program database (see reason 3) in the folder where the executable is located is out of date because VS puts the program database elsewhere on each compilation). Path where to put the .pdb file is set in the linker properties of the project.

sharptooth
+1  A: 

Do you attach the debugger to your running program? If so, it's possible that you've made a change to the source file, but have forgotten to compile the code. I will occasionally do this, although it's pretty easy to spot.

Travis