dlopen加载so动态链接库出现段错误的问题

rowen_n 2012-01-09 04:41:44
so库中暴露出来的函数(写在某基类头文件中)大体如下:
……
#ifdef __cplusplus
extern "C" {
#endif

Object* construct() {
return new Object;

}

void destroy(Object* object) {
delete object;
}

#ifdef __cplusplus
}
#endif
……

调用so的程序结构如下:
定义了一个函数(根据传入的so_name来执行相关操作):
void so_test(so_name, other_arg) {
……
handle = dlopen(so_name, RTLD_LAZY);
……
Type1* create = (Type1*)dlsym(handle, "construct");
Type2* destroy = (Type2*)dlsym(handle, "destroy");
……
Object* object = create();
……
destroy(object );
……
dlclose(handle);
}
在其它函数中调用如上函数,调用时有2种情况:
情况1(同一个函数中):
void func() {
so_test(so_name, other_arg1);
so_test(so_name, other_arg2);
}
情况2(在不同的函数中):
void func1() {
so_test(so_name1, other_arg);
}
void func2() {
so_test(so_name2, other_arg);
}

编译运行后,不管是哪一种情况,前一次调用so_test正常,后一次调用so_test就会出现段错误

弄了好几天都没弄好,快疯了,求大侠指点一下
...全文
1624 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
jinjianxinxh 2012-01-10
  • 打赏
  • 举报
回复
代码的问题吧
JoeBlackzqq 2012-01-10
  • 打赏
  • 举报
回复
最基本的调试技术:GDB单步调试。

如果代码不多,请帖出全部代码,说不定还能帮你调试下!
rowen_n 2012-01-10
  • 打赏
  • 举报
回复
继续求解答~
rowen_n 2012-01-10
  • 打赏
  • 举报
回复

gdb调试时运行和栈信息:

[Thread debugging using libthread_db enabled]
[New Thread 47066043974304 (LWP 11033)]
[==========] Running 1 test from 1 test case.
[----------] Global test environment set-up.
[----------] 1 test from FeaGenSoTest
[ RUN ] FeaGenSoTest.SO_ID_TEST
./feature_generator_so/group_gen_id.so <----第1次执行OK
after handle
after create and destory
after generator
after flatten
feature size: 1
feature 1 name: 1:0-2
feature 1 value: 1.000000
after destroy
./feature_generator_so/group_gen_id.so <----第2次执行就core了

Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread 47066043974304 (LWP 11033)]
__static_initialization_and_destruction_0 (__initialize_p=<value optimized out>, __priority=<value optimized out>)
at /usr/include/c++/4.1.2/bits/basic_string.h:1926
1926 const size_type __size = this->size();
(gdb) bt
#0 __static_initialization_and_destruction_0 (__initialize_p=<value optimized out>, __priority=<value optimized out>)
at /usr/include/c++/4.1.2/bits/basic_string.h:1926
#1 0x00002ace6ad5de72 in __do_global_ctors_aux () from ./feature_generator_so/group_gen_id.so
#2 0x00002ace6ad5668b in _init () from ./feature_generator_so/group_gen_id.so
#3 0x00002ace6ad550da in ?? () from ./feature_generator_so/group_gen_id.so
#4 0x00002ace69e7352b in call_init () from /lib64/ld-linux-x86-64.so.2
#5 0x00002ace69e73635 in _dl_init_internal () from /lib64/ld-linux-x86-64.so.2
#6 0x00002ace69e771e8 in dl_open_worker () from /lib64/ld-linux-x86-64.so.2
#7 0x00002ace69e731b6 in _dl_catch_error () from /lib64/ld-linux-x86-64.so.2
#8 0x00002ace69e76a8b in _dl_open () from /lib64/ld-linux-x86-64.so.2
#9 0x00002ace6a4961fa in dlopen_doit () from /lib64/libdl.so.2
#10 0x00002ace69e731b6 in _dl_catch_error () from /lib64/ld-linux-x86-64.so.2
#11 0x00002ace6a49658d in _dlerror_run () from /lib64/libdl.so.2
#12 0x00002ace6a496171 in dlopen@@GLIBC_2.2.5 () from /lib64/libdl.so.2
#13 0x00000000004076bf in FeaGenSoTest::TestSo (this=0x5922c0, so_name=@0x7fff40c3ed50, generator_name=@0x7fff40c3ed40)
at feature_generator_so_test.cpp:46
#14 0x0000000000407d95 in FeaGenSoTest_SO_ID_TEST_Test::TestBody (this=0x5922c0) at feature_generator_so_test.cpp:101
#15 0x0000000000448d0e in testing::internal::HandleSehExceptionsInMethodIfSupported<testing::Test, void> ()
#16 0x000000000045c310 in testing::internal::HandleExceptionsInMethodIfSupported<testing::Test, void> ()
#17 0x0000000000440c2d in testing::Test::Run ()
#18 0x0000000000447318 in testing::TestInfo::Run ()
#19 0x0000000000447462 in testing::TestCase::Run ()
#20 0x0000000000447746 in testing::internal::UnitTestImpl::RunAllTests ()
#21 0x000000000044932c in testing::internal::HandleSehExceptionsInMethodIfSupported<testing::internal::UnitTestImpl, bool> ()
#22 0x000000000045bd8a in testing::internal::HandleExceptionsInMethodIfSupported<testing::internal::UnitTestImpl, bool> ()
#23 0x0000000000440402 in testing::UnitTest::Run ()
#24 0x00000000004073cf in main (argc=1, argv=<value optimized out>) at feature_generator_so_test.cpp:153

dongjiawei316 2012-01-10
  • 打赏
  • 举报
回复
也就是说你的函数so_test能成功调用一遍,在第二遍的时候出的问题?那么就应该和你的so没有关系啊!你不用so,直接把代码放在一起编译试试?

23,217

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧