我有静态库链接到可执行文件。可执行文件本身不使用库符号。但是这个可执行文件在运行时加载了一些共享库,其中一个使用库中的符号。下面是库源文件的非常简化版本。
ParentClass.h
#include <iostream>
using namespace std;
class ParentClass {
ParentClass() {}
// some functionality
};
ChildClass.h
#include <ParentClass.h>
struct StaticData {
static const char *staticString;
};
class ChildClass : public ParentClass, public StaticData {
ChildClass() {}
// some extended functionality here
};
ChildClass.cpp
#include "ChildClass.h"
const char * StaticData::staticString = "string";
// functionality implementation
以下是几个事实:
1.编码如下:
ChildClass test;
//extended use of test functionality
效果很好。
2.像这样编码:
ChieldClass *test = new ChieldClass();
test->some_func(); // some func don't use dynamic memory
test->some_other_func(); // dynamic memory used (in my case malloc in gethostbyname system function)
在与库直接链接的二进制文件中使用时效果很好,但在运行时加载的共享库中使用时会失败并出现segfault "path_to_exec malloc(): memory corruption: some_address"
(参见开头的说明)。
3.像这样编码:
ParentClass *test = new ParentClass();
test->some_func();
test->some_other_func();
无处不在。
我无法理解为什么第2项中的代码会导致分段错误,但我怀疑在ChildClass中使用静态数据时遇到了麻烦(除了差异之外,ChildClass只定义了一些具有使用ParenClass函数的扩展功能的新函数即使我没有使用重载的ParentClass函数,也会发生分段错误。但我无法将这个单一的差异与仅在使用共享库中的ChildClass动态加载到我的库的可执行文件时发生的segnemtation错误这一事实联系起来。
我很高兴听到任何想法摆脱这种段错误。
更新:bt与std :: cout一起使用logger函数时(省略了一些名称)。呼叫顺序:
ChildClass *test = new ChildClass();
test->printInfo();
test->connect();
未在ChildClass中重新定义connect fnction。
(gdb) bt
#0 0x00007f756f67e165 in raise () from /lib/libc.so.6
#1 0x00007f756f680f70 in abort () from /lib/libc.so.6
#2 0x00007f756f6b427b in ?? () from /lib/libc.so.6
#3 0x00007f756f6bdad6 in ?? () from /lib/libc.so.6
#4 0x00007f756f6c0b6d in ?? () from /lib/libc.so.6
#5 0x00007f756f6c2930 in malloc () from /lib/libc.so.6
#6 0x00007f756f6af35b in ?? () from /lib/libc.so.6
#7 0x00007f756f7291de in ?? () from /lib/libc.so.6
#8 0x00007f756f72aa65 in __res_maybe_init () from /lib/libc.so.6
#9 0x00007f756f72ca70 in __nss_hostname_digits_dots () from /lib/libc.so.6
#10 0x00007f756f731fe4 in gethostbyname_r () from /lib/libc.so.6
#11 0x0000000000507929 in underlaying_c_code_connect (client=0x7f7564017348) at /home/beduin/???/lib/???/UnderlayingCCode.cpp:1477
#12 0x0000000000504a24 in ParentClass::connect (this=0x7f7564017340) at /home/beduin/???/lib/???/ParentClass.cpp:216
#13 0x00007f7569342f68 in Plugin::Start (this=0x7f75640208c0) at /home/beduin/???/plugins/???/Plugin.cpp:84
#14 0x00000000004c7d45 in ???::PluginHolder::StartPlugin (this=0x7fffed7dc5e0, it=@0x7fffed7dbad0) at /home/beduin/???/plugins.cpp:317
#15 0x00000000004c8656 in ???::PluginHolder::Start (this=0x7fffed7dc5e0) at /home/beduin/mrvs/framework/base/plugins.cpp:401
#16 0x00000000004c7935 in ???::PluginHolder::LockNLoad (this=0x7fffed7dc5e0) at /home/beduin/???/plugins.cpp:284
#17 0x00000000004afe6f in main (argc=3, argv=0x7fffed7dd978) at /home/beduin/???/main.cpp:148
使用自定义记录器:
#0 0x00007f824aa12165 in raise () from /lib/libc.so.6
#1 0x00007f824aa14f70 in abort () from /lib/libc.so.6
#2 0x00007f824aa4827b in ?? () from /lib/libc.so.6
#3 0x00007f824aa51ad6 in ?? () from /lib/libc.so.6
#4 0x00007f824aa54b6d in ?? () from /lib/libc.so.6
#5 0x00007f824aa56930 in malloc () from /lib/libc.so.6
#6 0x00007f824b2a46bd in operator new () from /usr/lib/libstdc++.so.6
#7 0x00007f824b280b29 in std::string::_Rep::_S_create () from /usr/lib/libstdc++.so.6
#8 0x00007f824b281aeb in std::string::_Rep::_M_clone () from /usr/lib/libstdc++.so.6
#9 0x00007f824b28205c in std::string::reserve () from /usr/lib/libstdc++.so.6
#10 0x00007f824b27c021 in std::basic_stringbuf<char, std::char_traits<char>, std::allocator<char> >::overflow () from /usr/lib/libstdc++.so.6
#11 0x00007f824b280215 in std::basic_streambuf<char, std::char_traits<char> >::xsputn () from /usr/lib/libstdc++.so.6
#12 0x00007f824b2763b5 in std::__ostream_insert<char, std::char_traits<char> > () from /usr/lib/libstdc++.so.6
#13 0x00007f824b27662f in std::operator<< <std::char_traits<char> > () from /usr/lib/libstdc++.so.6
#14 0x00000000004f4fb0 in ???::Logger::LogWriter::operator<< <char [25]> (this=0x7fff8e241fc0, str=@0x52b2fd)
at /home/beduin/???/log:184
#15 0x0000000000500388 in ChildClass::printInfo (this=0x7f8240017470) at /home/beduin/???/ChildClass.cpp:480
#16 0x00007f82446d6f5c in Plugin::Start (this=0x7f82400208a0) at /home/beduin/???/plugins/???/Plugin.cpp:83
#17 0x00000000004c7d35 in ???::PluginHolder::StartPlugin (this=0x7fff8e243b30, it=@0x7fff8e243020) at /home/beduin/???/plugins.cpp:317
#18 0x00000000004c8646 in ???::PluginHolder::Start (this=0x7fff8e243b30) at /home/beduin/???/plugins.cpp:401
#19 0x00000000004c7925 in ???::PluginHolder::LockNLoad (this=0x7fff8e243b30) at /home/beduin/???/plugins.cpp:284
#20 0x00000000004afe5f in main (argc=3, argv=0x7fff8e244ec8) at /home/beduin/???/main.cpp:148
答案 0 :(得分:2)
在valgrind(而不是gdb)下运行程序。它将向您显示无效内存访问发生的第一个位置,这可能与崩溃最终发生的位置不同。
关于作为共享库链接时它已损坏的事实,您使用的是-fPIC吗?如果没有,试试吧。