Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Missing non-virtual thunks" and inheritance order

Tags:

c++

gcc

thunk

We have a large code base in C++ and after a smallish refactor (one class added and some related methods rewritten), we started getting linker errors on GCC 3 & 4. The linker errors were specifically "missing references to non-virtual thunks" in small sample programs that subclassed classes in our large SDK.

Searching the web didn't give many hints beyond some old GCC bugs that seem to have been resolved.

The attributes of the problem seem to be:

  • GCC 3.4.6 & 4.3.3 optimizing with -O2
  • Multiple inheritance, including occasional virtual inheritance.
  • Changing the inheritance order from, say,
    class Foo: public A, public B {} to
    class Foo: public B, public A {}
    on the classes that are missing the thunks "fixes" the problem.

The virtual inheritance only appears in a single, very-commonly used base class for reference counting. I have verified that every usage of this class really is virtual public, and not just public inheritance by accident.

Obviously fiddling with the inheritance order is not really solving the problem. What else could it be?

like image 440
Adrian Avatar asked Apr 14 '11 15:04

Adrian


1 Answers

If changing the declaration order of the base classes fix the issue, it probably means that one of the base class doesn't properly define what it declared.

For example, if you have a declaration with a (not virtual) method Func in class A and the same in class B but you never defined it for class A, then the first time the method is called in your child, the class A's version is called, but at link time, the reference is not found. If you change inheritance order, will make the compiler call B::Func instead, which is defined and then the linker will find it.

IMHO, it's a bad design anyway, since there behaviour will be hard to predict and to debug.

like image 105
xryl669 Avatar answered Oct 12 '22 23:10

xryl669