Details
-
Bug
-
Resolution: Out of scope
-
P2: Important
-
None
-
5.10.0
-
None
-
(1) Ubuntu Linux 16.04/GCC; (2) Windows 10/Microsoft Visual Studio 2015; (3) Windows 10/Microsoft Visual Studio 2017. All with custom compiled versions of Qt 5.10.0 (compiled with the same compiler as the user code).
Description
If a static QColor is constructed using a colour name, this can fail unpredictably by giving an invalid QColor (e.g. in test code as below: works under Linux/GCC, fails under Windows/MSVC2015 or MSVC2017).
const QColor THE_COLOUR("purple"); // may work, may fail const QColor SAME_COLOUR(128, 0, 128); // always works void somefunc_called_after_main() { const QColor ANOTHER_COLOUR("purple"); // always fine }
In qcolor.cpp, the colour-name initializer ultimately comes through to get_named_rgb_no_space() which uses this private static variable in qcolor.cpp:
static const struct RGBData { const char name[21]; uint value; } rgbTbl[] = { { "aliceblue", rgb(240, 248, 255) }, // ... };
Therefore, I presume that the problem is that it's undefined by the C++ standard whether rgbTbl in qcolor.cpp has been initialized by the time that user code initializes its module-level variables. If it hasn't, colour-name-based initialization will fail.
This seems to be undocumented and can lead to unpredictable failures (with success/silent failure being related to the specific compiler in use and/or luck). Whilst there are obvious workarounds (e.g. just using RGB numbers), would it be possible to have qcolor.cpp initialize its static rgbTbl when first requested (e.g. as per https://isocpp.org/wiki/faq/ctors#static-init-order-on-first-use)?