Wrong values for local variables in vs2010 debugger - by Feng Peng

Status : 

  By Design<br /><br />
		The product team believes this item works according to its intended design.<br /><br />
		A more detailed explanation for the resolution of this particular item may have been provided in the comments section.

Sign in
to vote
ID 651936 Comments
Status Closed Workarounds
Type Bug Repros 1
Opened 3/17/2011 1:37:07 AM
Access Restriction Public


int _tmain(int argc, _TCHAR* argv[])
	int i = 5;
	int sum = 0;
	for (int i = 0; i < 10; ++i)
		sum += i;
	printf("%d\n", i);
	return 0;
set a breakpoint on the return statement, printf printed 5 and in watch window, value of i was 10!(VS2010, with or without SP1, debug version, no optimize, XP professional SP3)
Sign in to post a comment.
Posted by tkc5443 on 6/15/2011 at 5:18 AM

This is a cop out. Yes, we get that you extend the scope of variables for usability purposes. But if you do that, do it right, so that in case of a name conflict, the name with the correct non-extended scope wins.

This *is* a bug, so please fix it. If you insist in marking it "by design", nobody is stopping you, but please *fix* your design.
Posted by Microsoft on 3/18/2011 at 6:20 PM
Hello Feng.

I have resolved this bug as by design.
When the debugger displays variables in the watch window, and there are multiple variables with the same name, the debugger doesn't know which variable to choose. The one it chooses is arbitary, and - as in this case - may choose the "wrong" one.

You may notice that there is actually only one variable in scope at the return statement, and this would appear to contradict the reasoning above.

First, if you use /Zc:forscope-, the inner i really does continue to exist outside the for loop.

Second, the debugger actually "extends" the scope of the inner scope variable. The reason is usability. It is very useful to see what the loop counter ended on while debugging. So, at the point of the return statement, two 'i's are (to the debugger) in scope.

I hope that helps. Let me know if you have additional questions.

Lin Xu
Visual Studio
Posted by Helen [MSFT] on 3/18/2011 at 1:50 AM
Thanks for your feedback.

We are rerouting this issue to the appropriate group within the Visual Studio Product Team for triage and resolution. These specialized experts will follow-up with your issue.

Posted by Helen [MSFT] on 3/17/2011 at 2:23 AM
Thank you for your feedback, We are currently reviewing the issue you have submitted. If this issue is urgent, please contact support directly (http://support.microsoft.com/)
Posted by Microsoft on 3/17/2011 at 2:13 AM
Thank you for your feedback, we are currently reviewing the issue you have submitted. If this issue is urgent, please contact support directly(http://support.microsoft.com)