Be careful while watching variables with Visual Studio Debugger’s tooltip.

17 07 2008


We used to watch variables values by hovering mouse over them. So visual studio will show the variable value as tooltip. But beware, pitfalls are waiting for you, especially while watching structures.


If you just hover the mouse over a variable, Visual Studio will pick the variable name just below and shows its value as tooltip. But it can be dangerous. For instance, assume you’ve a structure, and local variable with name same as structure member. Check the code snippet.

// Some structure.
struct Drive
{
    BOOL Present;
};

void Function()
{
    // Local variable with same name
    // as structure member.
    BOOL Present = FALSE;

    // Structure with variable
    Drive CdDrive = { 0 };

    // If you watch the var by mouse hover,
    // the value displayed will be that of
    // local variable.
    CdDrive.Present = TRUE;
}

If you just mouse hover on the structure member variable, you’re going to deep trouble. The value displayed will be wrong. Since debugger choose just the variable name under the mouse, if a local variable of same name exists, the value of local variable will be displayed. Enough and more to get mis-guided. Have a look at the screenshot.

So always watch structure members by selecting the full variable name. See following screenshot.


Silly stuff. But it can grab a lot of time by blaming the compiler and rebuilding source again and again as I did. 😉

Targeted Audience – Intermediate.

Advertisements

Actions

Information

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s




%d bloggers like this: