c++全局对象引起程序core dump

当多个动态库中包含相同名字的全局对象时,会在exit的钩子多次注册,当程序退出时对象会析构多次,造成崩溃。

简单的例子:  
aaa.cpp  :

#include <string>
std::string msg("fdsafasfsa");

ccc.cpp:

int main(int argc,char* argv[]){
    return 0;
}

然后

$g++ -g -shared -o libaaa.so aaa.cpp
$g++ -g -shared -o libbbb.so aaa.cpp
$g++ -g -o ccc ccc.cpp -laaa -lbbb -L.
生成的ccc程序,执行就会core dump。

posted @ 2020-04-08 11:13  gelare  阅读(306)  评论(0)    收藏  举报