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.
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; }
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With