Home Dashboard Directory Help
Search

Wrong values for local variables in vs2010 debugger by Feng Peng


Status: 

Closed
 as By Design Help for as By Design


1
0
Sign in
to vote
Type: Bug
ID: 651936
Opened: 3/17/2011 1:37:07 AM
Access Restriction: Public
2
Workaround(s)
view
1
User(s) can reproduce this bug

Description

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)
Details
Sign in to post a comment.
Posted by tkc5443 on 6/15/2011 at 5:18 AM
@Lin:

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
linxu@microsoft.com
Posted by Microsoft 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 Microsoft 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)
Sign in to post a workaround.
Posted by Brad Sullivan on 3/18/2011 at 3:25 PM
Use the locals window. It will show you both values of i.
Posted by Feng Peng on 3/17/2011 at 1:37 AM
Don't define variables in inner scope with the same name in outer scopes.