0

I've got some breakpoints in Visual Studio, and whilst debugging in debug mode, they are being ignored.

This only occurs when variable is not used. EG

 string s = "10;
 string t = "Hi";
 return s;

In the above, it will not stop if I set a break point on the string t. Nor will it show 't' in the debug window. If I set a watch, the message in the Watch Window is

The name 't' does not exist in the current context

What I'm having to do as a work around is

 string s = "10;
 string t = "Hi";
 string z = t; //pointless but the compiler now let's me focus on it
 return s;

Please note, this happens for all variables throughout the entire program.

My own research suggests it's the JIT doing this; that it is optimizing the code for me and removing any overhead where it can, but I thought it would only do this in release mode? The point of me doing the above is so I can compare a new value against an old value as I'm re-factoring code.

My question is, can I turn off this compiler optimization?

This Visual studio 2010 debugger don't stop at Breakpoint? doesn't help.

All projects are .NET 4.0 (I know having different versions .NET can confuse the compiler)
Within the solution configruation, I have debug selected
In the Properties Window -> Debug is also set for Debug
In the Properties Window -> Build, has Optimize code unchecked

9
  • You are right, the compiler will even give you a warning about this, and that code will not be part of the generated IL.
    – qqbenq
    Commented May 28, 2014 at 12:51
  • Have you searched for this on SO's other posts regarding this? Commented May 28, 2014 at 12:51
  • I did a search, hence why I've understood it's the optimization by the compiler @SyedFarjadZiaZaidi. Why do you ask?
    – Dave
    Commented May 28, 2014 at 12:51
  • Because I have seen a couple of posts about problems in debugging in VS. Commented May 28, 2014 at 12:54
  • And what happens if you set the breakpoint on string s = "10; and when the debugger hit it, move to next line with F10 key? Does it hit the string t = "Hi"; line?
    – Kamil T
    Commented May 28, 2014 at 12:58

2 Answers 2

1

I guess that you store some debugger-related log in that variable. You can use the volatile keyword, it basically says "Don't you optimize me!" to the compiler:

volatile string t = "Hi";  // t won't be optimised.

The keyword states that this particular area in memory can be set by some other parties (f.e. another asynchronous code), so the compiler won't optimise it.

0
0

The answer was thanks to @MikeCheel who suggested I deleted the pdb files. It was then I noticed my debug folder was empty, but my release folder was full of files. Clearly, the issue had to be my configuration settings.

I found out where quite quickly now I knew what to look for:

So, I right click on the solution -> Properties

Under configuration properties, my project was set to Release under Debug.

This fixed it, sadly, my code is asserting every where now so at least I know it's working!

Not the answer you're looking for? Browse other questions tagged or ask your own question.