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.
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:
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 :)
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.