Failing qobject_cast
-
Hi guys, it might be because I am completely asleep... 3:15am
I thought of using the metaObject to verify my type beforehand.
Yet the className seems ok, but the cast fails... Any clue?@
virtual bool applyFilter(Model* model, RichParameterSet& par, CallBackPos* cb=0){
qDebug() << model->metaObject()->className();
SurfaceMeshModel* mesh = qobject_cast<SurfaceMeshModel*>(model);
if(!mesh){
qDebug("MASSIVE FAIL.. TERMINATING");
exit(-1);
return false;
}
....
}
@output:
@
SurfaceMeshModel
MASSIVE FAIL.. TERMINATING
@These are the core definitions:
@
class SurfaceMeshModel : public Model, public Surface_mesh{
Q_OBJECT
Q_INTERFACES(Model)
....
@@
class Model : public QObject{
Q_OBJECT
....
};
Q_DECLARE_INTERFACE(Model, "starlab.interface.Model/1.0")
@ -
[quote author="rcari" date="1319722285"]I know this is a weird question but are you sure your model is not NULL in the first place ?[/quote]
This is actually a good question.
[quote]Plus, why do you use qobject_cast here, when a static_cast would be much more efficient ?[/quote]qobject_cast() simply is safer. If this particular cast turns out to be a performance hog, then it is time to start thinking about static_cast()ing. Remember that premature optimization is the root of all evil, and that more programming sins are committed in the name of performance than any other, including blind stupidity.
-
[quote author="Franzk" date="1319726749"]
[quote author="rcari" date="1319722285"]I know this is a weird question but are you sure your model is not NULL in the first place ?[/quote]This is actually a good question.
[/quote]I don't think so since tallia1 calls model->metaObject(). If model == 0 the app would crash at this point, not later.
-
I just spent my morning trying to figure it out. Indeed rcari
is right, I didn't need to to qobject_cast. I was using it mostly
because I wanted to use introspection to see whether I was
doing something wrong...An important note is that the two classes are located in
different dynamically loaded libraries... so I am not sure
I can use the C++ dynamic_cast there...I will try what you suggest Volker.
-
-
And this is the output of this snippet:
@
const QMetaObject *mo = model->metaObject();
qDebug() << "Hierarchy: ";
while(mo) {
qDebug() << " " << mo->className();
mo = mo->superClass();
}
@Output:
@
Hierarchy:
SurfaceMeshModel
Model
QObject
MASSIVE FAIL.. TERMINATING
@ -
I am on OSX Lion. I isolated the problem even more. Look at the snippet below:
@
SurfaceMeshModel* model = new SurfaceMeshModel(path);Model* retval = qobject_cast<Model*>(model);
qDebug() << "Conversion Mesh=>Model: " << (retval?"success":"failed");SurfaceMeshModel* mesh = qobject_cast<SurfaceMeshModel*>(retval);
qDebug() << "Conversion Model=>Mesh: " << (mesh?"success":"failed");
@And the output is ...
@
Conversion Mesh=>Model: success
Conversion Model=>Mesh: failed
@ -
Weird. It's completely weird.
Just a another blind guess: may it be, that you happen to load two different versions of the Qt libs via the two libraries (resp. your app and library)? This may cause trouble too.
You can check this with the otool tool on the mac and some dyld debug settings:
@
export DYLD_PRINT_LIBRARIES=1
/path/to/your/Program.app/Contents/MacOS/Program 2>LOG-libraries.txt
unset DYLD_PRINT_LIBRARIES
@Then check LOG-libraries.txt for the loaded Qt libraries:
@
grep Qt LOG-libraries.txt
@It must not print libraries from different paths.