Does Visual Studio show incorrect values ​​when debugging?

I was debugging my code when I saw something strange. It basically came down to the following:

Weirdness

Here you see an unsigned int with a negative value ...

I also noticed that some surrounding variables did not have value pop-up (?), While others did. But this variable was the only one that had an impossible meaning.

How is this possible?

I only have C # Visual Studio 2010, so I don’t know that this problem exists in other versions or in different languages. I will change the title and tags accordingly, if so.

+4
source share
2 answers

During debugging, Visual Studio will track each variable in the current context.

Consider the following code snippet:

 void SomeMethod1() { int j = 0; } void SomeMethod2() { int i = 0; }//Breakpoint on this line. 

When the program is interrupted, the current context is SomeMethod2 . At this point, the developer cannot verify what the value of j . This is because int j not a variable in the current context.

The actual reason for the described behavior of the OP:

Visual Studio checks if the variable name exists in the current context, and does not check if the variable itself exists in the current context.

So, if we change the name of the variable j to i in SomeMethod1 , we can suddenly look at its “value”. Imagine how strange it would be if i in SomeMethod2 were a string:

enter image description here

When should you know this?

Whenever you have a piece of code where it does not immediately determine what the current context is. I ran into this problem in the following code snippet:

 private double CalculateFast(double d) { //Depending on the size of d, either [Method 1] or [Method 2] is faster. //Run them both and use the answer of the first method to finish. Tasks.Task.Factory.StartNew(() = > { //Method 1 } //Method 2 return answer; } 

I was debugging Method 2 , but I thought I could look at Method 1 variables. But this was not so, because Method 1 has its own context.

+7
source

Try:

  • Clean solution
  • Reconstruction
  • Failed: manually kill PDB files (or entire bin and obj folders) and rebuild

Hope this helps.

-one
source

Source: https://habr.com/ru/post/1495949/


All Articles