开发者

"Missing non-virtual thunks" and inheritance order

开发者 https://www.devze.com 2023-02-25 01:33 出处:网络
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 specifical

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?


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.


It could be the diamond problem

if so Check this thread

0

精彩评论

暂无评论...
验证码 换一张
取 消