Define a specific object type behind auto_ptr from the main dump

I have auto_ptr<IFoo> where IFoo is an interface with only pure virtual methods.

Now I also have the main file after the segmentation error, where I would really like to know that there was a specific subclass behind this auto-bird. Since dynamic_cast works in the project, I think RTTI should be somehow accessible, but I donโ€™t know how to access this information through gdb ?

The output I get is as follows:

 (gdb) print this->obj._M_ptr $22 = (class martin::IFoo *) 0x7418 

What I really would like to know if the pointer belongs to IBar or IBaz .

Thanks for any help!

+4
source share
1 answer

What I really would like to know if the pointer belongs to IBaror IBAZ

GDB should be able to tell you this. Use (gdb) set print object on . The documentation is here .

When displaying a pointer to an object, determine the actual (derived) type of the object, not the declared type, using the virtual function table. Please note that a table of virtual functions is required - this function can work only for objects that have identification type of runtime; one virtual method in the declared object type is enough.

Update:

it displays the IFoo * interface

This probably means that the pointer does point to IFoo (for example, an object that was of type IBar or IBaz was already destroyed).

Will working with dynamic_cast mean

Yes, dynamic_cast cannot work without RTTI; if you use dynamic_cast , print object on should just work.

+5
source

Source: https://habr.com/ru/post/1440315/


All Articles