So the code mentioned on the topic line causes segmentation fault with Qt 4.8.3 & gcc 4.7.2
This is at outside of any classes/structs at .cpp-file and works with gcc 4.4
const QList<int> warnings = QList<int>() << 0 << 3 << 7;
Traces gives these two hints:
__do_global_ctors()
__static_initialization_and_destruction_0
So it seems that "warning" is not yet available when its inserting latter list into it.
Works with 4.7.2 if i change it into this:
global scope: QList< int> warnings;
This is inisde some function:
warnings = QList<int>() << 0 << 3;
I'm wondering why this happens?
Edit:
I guess i clipped a bit too much stuff out from my question originally, but warnings is supposed to be const at file scope (.cpp-file) for holding bunch enums.
My psychic debugging powers tell me that the line in question exists at global/file scope, not at class/function scope. Thus your line may be called at any point during static initialization. With your old gcc it just so happened that QT was initialized before your line was called. With the new gcc it reordered (perfectly legal) the static init to call your function first, before QT was ready to create objects and insert into them.
The solution is to hold off on creating that QList
until after main
starts. Using pointers or static local objects are two common implementations.
来源:https://stackoverflow.com/questions/13270738/const-qlistint-warnings-qlistint-0-segfaults-with-gcc-4-7-2