Search code examples
c++visual-studio-2010debuggingremote-debugging

VS2010 debugger in C++, meaning of "unused=???" or "unused=0"


Debugging in Visual Studio 2010 (C++, unmanaged), what is the information unused ??? or unused 0 supposed to mean? I have attached two screenshots, child is a HWND to an existing window.

I am also confused by the fact, that the HWND is sometimes displayed as "unused", sometimes as "0". When having a pointer referring to unallocated memory, I'd understand the situation, but in my particular case the window is already created and valid.

VS2010 unused=???

VS2010 unused=0


Solution

  • This goes back to the early nineties, back when there was just a HANDLE as a type to declare a handle of any Windows object. Which was a bug factory, programmers could fumble code and, say, pass a font handle where a window handle was required. So the STRICT macro was added later, it redeclared handle types so mixing produces a compiler error. Which looks like this in winnt.h, edited for content:

    #ifdef STRICT
       typedef void *HANDLE;
       #define DECLARE_HANDLE(name) struct name##__{int unused;}; typedef struct name##__ *name
    #else
       typedef PVOID HANDLE;
       #define DECLARE_HANDLE(name) typedef HANDLE name
    #endif
    

    Example usage:

    DECLARE_HANDLE(HWND);
    

    The struct macro soup ensures that a HFONT can never be used where a HWND is expected when STRICT is turned on, it produces a type mismatch on the structure type.

    You can now see where "unused" comes from. It is in fact unused, only Windows can create handle values. It is helpful in the debugger since it lets you look at the handle value, with 0 or -1 being a sure sign of trouble.

    More about STRICT in this MSDN article.