[Solved] qobject_cast fails when accessed across DLL boundaries.
I defined a simple class in a static lib shared between a plugin and a host exe. Yes, the class derives from QObject has the Q_OBJECT macro and all of that. It's used in a publish / subscribe feature defined in a static lib that forms a generic communication method between an app and plugins.
What I have noticed is that qobject_cast in the exe works for instances create in the exe bug not for instances created in a plugin. qobject_cast will also work in the plugin for classes instantiated there. Instances going across the plugin to the exe fail for qobject_cast, but work for dynamic_cast.
Is qobject_cast supposed to not work when used for class instances instantiated across DLL boundaries?
Hi and welcome to devnet,
Lets call your QObject derived class MyObject.
Your current problem is not the dll boundaries, it's your static library. Each element of your project that links to that static library will have it's own copy of your MyObject class. This means that you will have multiple copies of the static meta object associated to your class thus qobject_cast will fail since it will check the plugin MyObject staticMetaObject against the application MyObject staticMetaObject. IIRC using gcc there's a workaround for that. But the simple and straight forward way is to keep your common code in a shared library
I came across an old thread that said the same or similar to what you just said. I have since moved the QObject derived class into a shared DLL and qobject_cast seems to now be working across DLL boundaries.
Thanks for your reply which is the answer to this problem.