Visual Studio 2008 – When Debugging Highlights the Wrong Line

The Twilight Zone

The weirdest problem. You debug your code in Visual Studio, place a break point and try to step through… All of a sudden, Visual Studio seems to go mad and highlights the wrong line. As you step further in it seems to jump almost randomly from one line to the other…. WTF?!!

Well, we’ve just had this problem with one of my clients.

Debugging the Debugger

The problem is sporadic – in other words, it’s always the same areas of the code where we get the issues, but we don’t always need to debug through those areas, so we don’t always notice.

Initially we thought it was debug symbol files (.pdb) being out of date/sync with the assemblies we’re building. This culminated in very lengthy complete teardown, clean and rebuilds, to no avail. Much googling and trial and error and wheelspinning.

A few articles on the internet mention a hotfix (KB937122) for Visual Studio 2005 SP1 that resolves a similar sounding problem, but this nothing relevant to our version of Visual Studio.

The next time the problem occurred, we thought maybe Visual Studio was loading the wrong .pdb files. But no. A check of Debug –> Windows –> Modules shows which symbol files are loaded and from where.

Click to Zoom! Visual Studio 2008 - Modules Window

We tried specifying locations to load symbols from, and even right-clicking modules in the above window, and picking Load Symbols From –> Module Path. None of this worked.

On and off, this has cost my team maybe 5 days of effort in the last couple of weeks.

The Culprit

After a lot of digging around, it transpires that if you tell the compiler to optimise your code, it can screw up the .pdb files; moreover it seems to cause the random stepping from line to line when debugging in Visual Studio 2008. The default setting in Visual Studio 2008 is for Release configurations to be optimised, but Debug configurations to be unoptimised. However, opening up many of our Visual Studio Project files shows something like this:

Click to Zoom! Build Options for Debug in Visual Studio 2008

The Resolution

So, someone, it seems, is guilty of setting the optimise flag on each of our 20-odd projects in Visual Studio. Luckily, we have Team Foundation Server as a source code repository, and I can see exactly who made that change, and when :)

Click to Zoom! Team Foundation Server Version History - Compare Versions

Maybe you’ve found this article via a search engine, in which case I hope I’ve saved you from the pain in the backside this issue caused us.

Comments

  1. Audrey Hood says

    Thank you so much – I spent all Friday afternoon trying to work out why VS was stepping into an if statement and then on into the else section as well. I am using VS 2005 but there is a similar ‘enable optimizations’ option in the advanced compile options – when I unticked that the problem resolved itself!

  2. ManInMoon says

    Great. This helped me solve my similar issue. My debug mode was already unoptimised, but I was debugging with “Release” selected in Standard toolbar. When I switched to debug there – the problem went away – thanks very much.

  3. Dishum says

    My VS 2008 shows different screens and they display optimze at two locations:
    whole opmization under conifguration tab and other one is under C++. Which one is the right one to do?

  4. Suamere says

    Grrrr. This is happening to me in 2010. The option is in “Compile > Advanced” in 2010, but the box is not checked. I also have been through the pdb hunt for the last day or so. Doing roll-backs, commenting code, manual symbol loading. I’m about to tear my hair out.

    • Suamere says

      Wow, so much for the last 24 hours. For anybody else getting here… I was having all the exact same symptoms described here, but the culprit was actually a javascript “Window.Print()” in the aspx source. My jumping issue was in an ashx file being called by the same page. So that JavaScript was, for some reason, causing my ashx file to freak out? I took it out and no more debug jumping… put it back in and the debug jumping started again. No clue why. I’ll have to find a new way to print the window from client-side.

Trackbacks

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>