Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debugging data in 'anonymous namespaces' (C++)

Recently, I got a crash dump file from a customer. I could track the problem down to a class that could contain incorrect data, but I only got a void-pointer to the class, not a real pointer (void-pointer came from a window-property, therefore it was a void-pointer). Unfortunately, the class to which I wanted to cast the pointer to, was in an anonymous namespace, like this:

namespace
   {
   class MyClass
      {
      ...
      };
   }

...
void *ptr = ...
// I know ptr points to an instance of MyClass,
// and at this location I want to cast ptr to (MyClass *) in the debugger.

When I use ptr in the watch window, Visual Studio 2005 just shows the pointer value. If I use (MyClass *)ptr, the debugger tells me it cannot cast to it.

How can I cast ptr to a MyClass pointer?

Note: I could eventually use a silly-named namespace (like the name of the source file), and then use a "using namespace", but I would expect better solutions.

like image 396
Patrick Avatar asked Aug 26 '09 14:08

Patrick


2 Answers

Referencing anonymous namespaces in Visual Studio Debugger's expressions is not supported (at least as of VS 2017) and it's really annoying.

From https://docs.microsoft.com/en-us/visualstudio/debugger/expressions-in-the-debugger#c-expressions

Anonymous namespaces are not supported. If you have the following code, you cannot add test to the watch window:

namespace mars
{   
    namespace  
    {  
        int test = 0;   
    }   
}   
int main()   
{   
    // Adding a watch on test does not work.   
    mars::test++;   
    return 0;   
}
like image 138
Glen Knowles Avatar answered Sep 24 '22 01:09

Glen Knowles


This is mentioned in MSDN. It doesn't look like there's a nice solution within the Watch window (you can get the decorated name of your class from a listing I guess).

Your "silly-named namespace" idea would work okay, you could also just declare an identical class with a silly name and cast to that type instead.

like image 41
Sumudu Fernando Avatar answered Sep 24 '22 01:09

Sumudu Fernando