C++ calling completely wrong (virtual) method of an object

后端 未结 4 2122
鱼传尺愫
鱼传尺愫 2021-02-19 03:43

I have some C++ code (written by someone else) which appears to be calling the wrong function. Here\'s the situation:

UTF8InputStreamFromBuffer* cstream = foo();         


        
4条回答
  •  有刺的猬
    2021-02-19 04:17

    This is happening because different source files disagree on the vtable layout of the class. The code calling the function thinks that readDocument(InputStream &, const wchar_t *) is at a particular offset, whilst the actual vtable has it at a different offset.

    This usually occurs when you change the vtable, such as by adding or removing a virtual method in that class or any of its parent classes, and then you recompile one source file but not another source file. Then, you get incompatible object files, and when you link them, things go boom.

    To fix this, do a full clean and rebuild of all of your code: both the library code and your code that uses the library. If you don't have the source code to the library but you do have the header files for it with the class definitions, then that is not an option. In that case, you cannot modify the class definition -- you should revert it to how it was given to you and recompile all of your code.

提交回复
热议问题