const QList<int> warnings = QList<int>() << 0; segfaults with gcc 4.7.2

梦想的初衷 提交于 2019-12-07 10:23:13

问题


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.


回答1:


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!