Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/157.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 共享库中静态函数成员的销毁顺序_C++_Destructor_Static Variables - Fatal编程技术网

C++ 共享库中静态函数成员的销毁顺序

C++ 共享库中静态函数成员的销毁顺序,c++,destructor,static-variables,C++,Destructor,Static Variables,我目前正在探索Boost.Serialization中一个非常棘手的bug,它与单例相关。对于上下文:Boost 1.65更改了单例中断is_destructed通知的实现,该通知导致程序退出或库卸载时出现SEGFULTS。Boost 1.66“修复”了此问题,但会泄漏内存 单例代码(与此问题相关)归结为: template<class T> struct singleton{ T& inst(){ static T t; return

我目前正在探索Boost.Serialization中一个非常棘手的bug,它与单例相关。对于上下文:Boost 1.65更改了单例中断
is_destructed
通知的实现,该通知导致程序退出或库卸载时出现SEGFULTS。Boost 1.66“修复”了此问题,但会泄漏内存

单例代码(与此问题相关)归结为:

template<class T> struct singleton{
    T& inst(){
        static T t;
        return t;
    }
}
可以看出,这会破坏Boost 1.65中的内存。原因是我通过劫持和记录ctor/dtor调用跟踪的混乱顺序:

ctor 0x7f9f0aa074a0 std::multiset<const extended_type_info_typeid_0*>
ctor 0x7f9f0a7f63e0 extended_type_info_typeid<float>

ctor 0x7f9f0a7f64a0 std::multiset<const extended_type_info_typeid_0*>
ctor 0x7f9f0aa073e0 extended_type_info_typeid<int>

dtor 0x7f9f0aa073e0 extended_type_info_typeid<int>
dtor 0x7f9f0aa074a0 std::multiset<const extended_type_info_typeid_0*>
dtor 0x7f9f0a7f64a0 std::multiset<const extended_type_info_typeid_0*>
dtor 0x7f9f0a7f63e0 extended_type_info_typeid<float>
ctor 0x7f9f0aa074a0标准::多集
ctor 0x7f9f0a7f63e0扩展类型信息类型ID
扇区0x7f9f0a7f64a0标准::多集
ctor 0x7f9f0aa073e0扩展类型信息类型ID
数据或0x7f9f0aa073e0扩展类型信息类型ID
数据或0x7f9f0aa074a0标准::多集
数据或0x7f9f0a7f64a0标准::多集
数据或0x7f9f0a7f63e0扩展类型信息类型ID
这是使用GCC 6.4,但与GCC 7.1相同。正如您所看到的,在第二个
扩展的\u type\u info\u typeid
之前,两个多集一起被销毁

我遗漏了什么吗?是C++允许的吗?

由:

如果构造函数的完成或 具有静态存储持续时间的对象在 另一个是,第二个析构函数的完成顺序 在第一个的析构函数启动之前

此外:

对于数组或类类型的对象,该对象的所有子对象都是 在任何具有静态存储持续时间的块作用域对象之前销毁 在构造子对象期间初始化的对象将被销毁


这有什么帮助?第二个地图不应该在第二个type_info之前被销毁。解决这个问题(以及许多其他相关问题)的方法:不要使用单例。它们是一种反模式,导致的问题往往比它们解决的问题多得多。该标准没有提到任何关于动态加载库的内容。
ctor 0x7f9f0aa074a0 std::multiset<const extended_type_info_typeid_0*>
ctor 0x7f9f0a7f63e0 extended_type_info_typeid<float>

ctor 0x7f9f0a7f64a0 std::multiset<const extended_type_info_typeid_0*>
ctor 0x7f9f0aa073e0 extended_type_info_typeid<int>

dtor 0x7f9f0aa073e0 extended_type_info_typeid<int>
dtor 0x7f9f0aa074a0 std::multiset<const extended_type_info_typeid_0*>
dtor 0x7f9f0a7f64a0 std::multiset<const extended_type_info_typeid_0*>
dtor 0x7f9f0a7f63e0 extended_type_info_typeid<float>