Initializing the QT MetaObject system in QT5
-
Hi,
I am (ab)using the QT meta object system to initialize static objects in my .CPP files (OS X, QT 5.15.2)For example I am calling staticMetaObject.className() of a class in a .cpp file, for example ( abbreviated for clarity)
static QString classname=staticMetaObject.className();
I am running afoul of C++ static initialization order. That is the static metaObject data stored in a generated moc_<classname>.cpp file is not initialized when the static member in "my". cpp is called so staticMetaObject.className() is garbage and the software crashes.
If, for example, I move my so-called "static" initialization to a function called after main() starts , then all is good because all the moc_ metadata is initialized and present.
Oddly this is a problem in QT5, not QT6 but my code has to run in both QT5 and QT6
Andrew
-
Hi,
I am (ab)using the QT meta object system to initialize static objects in my .CPP files (OS X, QT 5.15.2)For example I am calling staticMetaObject.className() of a class in a .cpp file, for example ( abbreviated for clarity)
static QString classname=staticMetaObject.className();
I am running afoul of C++ static initialization order. That is the static metaObject data stored in a generated moc_<classname>.cpp file is not initialized when the static member in "my". cpp is called so staticMetaObject.className() is garbage and the software crashes.
If, for example, I move my so-called "static" initialization to a function called after main() starts , then all is good because all the moc_ metadata is initialized and present.
Oddly this is a problem in QT5, not QT6 but my code has to run in both QT5 and QT6
Andrew
@AndrewCQT said in Initializing the QT MetaObject system in QT5:
Oddly this is a problem in QT5, not QT6 but my code has to run in both QT5 and QT6
Just by accident - the order is not defined.
Re-write your code to not use those global statics.