Visual Studio fails to display some watched expressions

Why is this happening?

The tool has its limitations. For example, many times I "go to definition" and the definition is not found. I have to "find in files". It is no surprise that some expressions are not evaluated during debugging sessions, either.

How do you fix it?

  • Keep expressions simple. Do not concatenate them directly, use variables with explanatory names for intermediate results.
  • Support your code with explicit assertions. If it's "wrong", an assertion should fail.

The errors you have are due to limitations in the debugger, there are not bugs as Daniel implies.

The watch window cannot call overloaded operators. If you have e.g. a std::vector<int> vecSomething you cannot put vecSomething[0] into the watch window, because std::vector<int>::operator[] is an overloaded operator. Consequently, for a vector of objects, you cannot do vecObject[0].SomeMemberVariableOfObject in the watch window. You could write vecObject._Myfirst[0].SomeMemberVariableOfObject. In Visual Studio's STL implementation, _Myfirst is a member of vector pointing at the first element.

If you add your own variables and types to the watch window, add watches to the data members directly. It is no problem to follow chains of pointers like member.memberStruct.ptrToObj->memberOfObj.

Edit:

Actually Visual Studio can call code in the Watch window: http://geekswithblogs.net/sdorman/archive/2009/02/14/visual-studio-2008-debugging-ndash-the-watch-window.aspx

Thus, it is slightly mysterious why overloaded operators cannot be used.