Mon 26 Dec 2016 02:05:13 AM UTC, original submission:
Just discovered a bug with initialization of globals in QtLua 2.1 that prevents most of QtLua from working. After some debugging I think I found the problem, although I admit I do not fully understand it:
qtluaqtlib.cc:103 defines a static variable qt_meta, calling the QMetaObjectTable::QMetaObjectTable() constructor that loops over meta_object_table. meta_object_table itself is defined as a global in qtluaqmetaobjecttable.cc. Now, when QMetaObjectTable::QMetaObjectTable() executes, most of the _mo pointers inside meta_object_table are NULL. I suspect that this is because meta_object_table is not yet fully initialized when the constructor is called (static initialization order between different module files is undefined in C/C++!). This leads to the loop inside the constructor aborting prematurely.
I'm not entirely sure what's going on, because SOME of the _mo pointers (in particular, all of the QtLua::*::staticMetaObject ones) are valid. It's only the Qt builtin ones that are not. Also, when I loop over meta_object_table at a later time in my program, the values ARE valid, just not at the time that QMetaObjectTable::QMetaObjectTable() is called.
A quick and dirty fix for this that works only with GCC is to use _attribute_((init_priority(XXX))) for the meta_object_table and qt_meta globals, forcing meta_object_table to be initialized first. It might be best though to move those two globals into the same .cc file, so that their initialization order is well defined for all compilers.
Tested it on QtLua 2.1, Qt 5.7.1 with MinGW-W64 GCC 5.3.0 on Windows. Maybe a bug that shows only on recent compilers because their initialization order has changed?
|