Saturday, April 13, 2013

Debug: Cannot watch a variable value

  One day, my colleague call me for help. He cannot figure out why he cannot watch a variable value during step debug. The similar codes are shown as  following:
String[] serverNames = SQLServers.GetNames();
He sets a break point at that particular line and hint shows nothing. He thinks maybe something wrong with the GetNames method. So he changes the code to a simple one:
int b1 = 1;
It shall show some thing in step debug, right? It turns out shows nothing at all. Just like the original code. Then he creates another solution, a very simple console application, with the following codes:
int a1 = 2;
int b1 = a1;
He sets the break point at a1 line and do a step over. The watcher and hint do show a1's value. Now he wonder it is a Visual Studio bug or not? He asks me to try it on my system if it is his system problem or not. Unfortunately, my system shows the same thing. I talk to my colleague that compiler may not show the value if it does optimum. However, he says it runs in debug option not release option, the code will not optimal. That's true.
  I keep pondering on  the debug mode and the optimum. Suddenly I remember one thing about his coding style. He loves to ship his code with debug option. As you know the debug code is not optimum. Therefore, I suggest him to check  "Optimize Code" option in Build tab. Since it is a long time story, we forget it completely. After I pin up the problem. The puzzle is solved. 

No comments:

Post a Comment