Dynamic_cast returns NULL but should not

I have the following class hierarchy:

class IStorage { [...] } Q_DECLARE_INTERFACE(IStorage, "ch.gorrion.smssender.IStorage/1.0") class ISQLiteStorage: public IStorage { Q_INTERFACES(IStorage) [...] } Q_DECLARE_INTERFACE(ISQLiteStorage, "ch.gorrion.smssender.ISQLiteStorage/1.0") class DASQLiteStorage: public QObject, public ISQLiteStorage { Q_OBJECT Q_INTERFACES(ISQLiteStorage) [...] } 

I am using QT and trying to create a plugin (for my application) with QtPlugin. I create an instance of DASQLiteStorage, and I pass this instance to the FROM WITHIN object of the plugin:

 // the next line is within my main app. // storage is the DASQLiteStorage instance. // gateway is an object from within the plugin. gateway->setDefaultStorage(storage); // this method lies within the plugin void AbstractGateway::setDefaultStorage(IStorage* storage) { defaultStorage_ = dynamic_cast<ISQLiteStorage*>(storage); } 

The problem is that dynamic_cast returns me a null pointer (not expected) when dynamic_cast is executed in my main application (ie until "gateway-> setDefaultStorage (storage);") gives me a valid pointer (expected).

Does anyone know why this might happen? Does the program work in a different memory range as a plugin? Could this lead to such problems? Any ideas how to fix this?

Thanks a lot!


EDIT: I tried some suggestions:

 // this method lies within the plugin void AbstractGateway::setDefaultStorage(IStorage* storage) { ISQLiteStorage* s = dynamic_cast<ISQLiteStorage*>(storage); s = static_cast<ISQLiteStorage*>(storage); s = qobject_cast<ISQLiteStorage*>((QObject*)storage); defaultStorage_ = s; } 

In the first line of the method, s is NULL, in the second, s contains the correct pointer, and in the third, another pointer. Why are these pointers not equal? And why dynamic_cast is still not working, although I am currently using:

 pluginLoader()->setLoadHints(QLibrary::ResolveAllSymbolsHint | QLibrary::ExportExternalSymbolsHint); 




EDIT2: I noticed that the segmentation error associated with this, which I got a little further in the code. I have the following construct:

 // The following classes are defined within the main app. class ILoginAccount: public IAccount [...] class AbstractAccountStroageOfficer { public: AbstractAccountStroageOfficer(IAccount* account)[...] } // These classes are defined within my plugin and are created from within the plugin. class BCAccount: public ILoginAccount { public: BCAccount() : ILoginAccount(new DAAccountStorageOfficer(this)) {}; } class DAAccountStorageOfficer: public AbstractAccountStorageOfficer { public: DAAccountStorageOfficer(ILoginAccount* account) : AbstractAccountStorageOfficer(account) // This line raises a segfault. { IAccount* a = account; // This line raises a segfault as well. a = dynamic_cast<IAccount*>(account); // This as well. a = static_cast<IAccount*>(account); // This as well. } } 

Should these segmentation errors not occur if they? But why are they?

+4
source share
1 answer

In principle, RTTI is not reliable across module boundaries. Different compilers have different behavior here; You will have to examine how your compiler / version works in this case. Of course, if you have another compiler / version for the main application and the plugin, it obviously does not have the ability to work.

Use static_cast to work.

+4
source

All Articles