链接到两个第三方共享库时,程序崩溃

前端之家收集整理的这篇文章主要介绍了链接到两个第三方共享库时,程序崩溃前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有两个用于 linux平台的外包共享库(没有源代码,没有文档).当它们分别链接到程序时(g xx.cpp lib1.so或g xx.cpp lib2.so),这些库工作正常.

但是,当任何c程序同时链接到这两个共享库时,程序不可避免地会因“双重释放”错误而崩溃(g xx.cpp lib1.so lib2.so).

即使c程序是一个空的hello world程序并且与这些库无关,它仍然会崩溃.

#include <iostream>
using namespace std;
int main(){
     cout<<"haha,I crash again. Catch me if you can"<<endl;
     return 0;
}

Makefile文件

g++ helloword.cpp lib1.so lib2.so

我得到了一些线索,这些lib1.so lib2.so库可能共享一些常见的全局变量,并且它们会破坏一些变量两次.我尝试过gdb和valgrind,但是无法从backtrace中提取有用的信息.

有什么方法可以隔离这两个共享库并使它们以沙盒方式工作?

EDITED(添加核心转储和gdb回溯):

我只是将前面提到的玩具空helloword程序与两个库(平台:带有gcc4.8.2的centos 7.0 64bits)联系起来:

g++ helloworld.cpp  lib1.so lib2.so -o check

Valgrind的:

==29953== Invalid free() / delete / delete[] / realloc()
==29953==    at 0x4C29991: operator delete(void*) (in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so)
==29953==    by 0x613E589: __cxa_finalize (in /usr/lib64/libc-2.17.so)
==29953==    by 0x549B725: ??? (in /home/fanbin/InventoryManagment/lib1.so)
==29953==    by 0x5551720: ??? (in /home/fanbin/InventoryManagment/lib1.so)
==29953==    by 0x613E218: __run_exit_handlers (in /usr/lib64/libc-2.17.so)
==29953==    by 0x613E264: exit (in /usr/lib64/libc-2.17.so)
==29953==    by 0x6126AFB: (below main) (in /usr/lib64/libc-2.17.so)
==29953==  Address 0x6afb780 is 0 bytes inside a block of size 624 free'd
==29953==    at 0x4C29991: operator delete(void*) (in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so)
==29953==    by 0x613E589: __cxa_finalize (in /usr/lib64/libc-2.17.so)
==29953==    by 0x4F07AC5: ??? (in /home/fanbin/InventoryManagment/lib2.so)
==29953==    by 0x5039900: ??? (in /home/fanbin/InventoryManagment/lib2.so)
==29953==    by 0x613E218: __run_exit_handlers (in /usr/lib64/libc-2.17.so)
==29953==    by 0x613E264: exit (in /usr/lib64/libc-2.17.so)
==29953==    by 0x6126AFB: (below main) (in /usr/lib64/libc-2.17.so)

gdb回溯消息:

(gdb) bt
#0  0x00007ffff677d989 in raise () from /lib64/libc.so.6
#1  0x00007ffff677f098 in abort () from /lib64/libc.so.6
#2  0x00007ffff67be197 in __libc_message () from /lib64/libc.so.6
#3  0x00007ffff67c556d in _int_free () from /lib64/libc.so.6
#4  0x00007ffff7414aa2 in __tcf_0 () from ./lib1.so
#5  0x00007ffff678158a in __cxa_finalize () from /lib64/libc.so.6
#6  0x00007ffff739f726 in __do_global_dtors_aux () from ./lib1.so
#7  0x0000000000600dc8 in __init_array_start ()
#8  0x00007fffffffe2c0 in ?? ()
#9  0x00007ffff7455721 in _fini () from ./lib1.so
#10 0x00007fffffffe2c0 in ?? ()
#11 0x00007ffff7debb98 in _dl_fini () from /lib64/ld-linux-x86-64.so.2
Backtrace stopped: prevIoUs frame inner to this frame (corrupt stack?)

更新

感谢@RaduChivu的帮助,我发现了一个非常类似的场景:segmentation fault at __tcf_0 when program exits,看起来确实两个库之间存在全局变量冲突.考虑到我没有这两个外部共享库的源文件,除了使用两个单独的进程外,还有其他方法可以解决这个冲突吗?

解决方法

一种可能的解决方案是永远不要叫退出.要终止程序,只需调用_exit即可.如果您需要做任何具体的事情,通常会通过退出来完成,只需在调用_exit之前自己完成.
原文链接:https://www.f2er.com/c/118639.html

猜你在找的C&C++相关文章