How to watch vtable in gdb in a C ++ program?

I have an interesting problem in one of my C ++ programs. Apparently, the vtable of one of my classes / gets corrupted during program execution. In a gdb session, I found that if I call an object method directly, it succeeds, but if I use a pointer or a link, I get a destructor of a completely unrelated class that will not be created in the near future. Without changing this -Pointer, of course.

Assuming my observations are correct, how can I watch a vtable object in gdb? I use Linux with gcc, my gdb version is GNU gdb (Ubuntu/Linaro 7.3-0ubuntu2) 7.3-2011.08 .

+4
source share
2 answers

You can use the -fdump-class-hierarchy gcc option, which will give you vtable information, however the output can be very verbose and hard to read.

For example, given the following trivial classes:

 class Base { public: virtual int method() = 0; }; class Derived : public Base { public: int method() { return 10; } }; 

corresponding conclusion

 Vtable for Base Base::_ZTV4Base: 3u entries 0 (int (*)(...))0 8 (int (*)(...))(& _ZTI4Base) 16 (int (*)(...))__cxa_pure_virtual Class Base size=8 align=8 base size=8 base align=8 Base (0x7f14c308ccc0) 0 nearly-empty vptr=((& Base::_ZTV4Base) + 16u) Vtable for Derived Derived::_ZTV7Derived: 3u entries 0 (int (*)(...))0 8 (int (*)(...))(& _ZTI7Derived) 16 (int (*)(...))Derived::method Class Derived size=8 align=8 base size=8 base align=8 Derived (0x7f14c2ee7208) 0 nearly-empty vptr=((& Derived::_ZTV7Derived) + 16u) Base (0x7f14c308cd20) 0 nearly-empty primary-for Derived (0x7f14c2ee7208) 

This should give you an idea of ​​what address ranges should be expected during debuggng, etc.

+3
source

If you do not hack, I doubt your vtable is confused. Are you calling a virtual function from the cost constructor?

It is also possible that the debugger is messing with you. Compilation with optimization, you can get the same address for functions that do the same as for code duplication. I came across this under Windows, where Visual Studio also jumped into different functions that actually did the same thing. Try issuing something instead of going through code with a debugger ... It could be .

0
source

All Articles