从特定库实例化对象时出现分段错误
我有一个 C++ 应用程序(大幅缩短,如下所示);
#include <iostream>
#include "MyClass.h"
void foobar()
{
MyClass a;
}
int main(int argc, char** argv)
{
std::cout << "Hello world!\n";
return 0;
}
其中“MyClass”在静态链接库 (.a) 中定义。
但是,该应用程序在启动时立即出现段错误,并且我从未进入“Hello world”。
我可以从同一个库创建接口的实例,但无法创建实现该接口的类的实例。 IE;
void foobar()
{
IMyClass a; // Having this in the application works.
MyClass b; // Segfault if this is in.
}
从上面可以看出,甚至不需要调用代码即可使应用程序出现段错误。
我正在使用 Netbeans 6.7.1 和 GCC 4.3.2。
现在,我假设库的链接有问题,但我不知道是什么。我也在其他库中链接(所有库都是静态链接的)。上面的类来自第一个链接库(至少是列表中的第一个)。如果我从第二个列出的库创建类的实例,则一切运行正常。
该问题可能与我的其他问题类似(或相关): https://stackoverflow.com/questions/1844190/linking-with-apache-xml-security-causes-unresolved-references
有人对可能出现的问题有任何建议吗?
I have an C++ application (heavily shortened down, shown below);
#include <iostream>
#include "MyClass.h"
void foobar()
{
MyClass a;
}
int main(int argc, char** argv)
{
std::cout << "Hello world!\n";
return 0;
}
Where "MyClass" is defined in a statically linked library (.a).
However, this application Segfaults the instant its started, and I never get to the "Hello world".
I can create an instance of an interface from the same library, but I cannot create an instance of a class that implements the interface. I.e;
void foobar()
{
IMyClass a; // Having this in the application works.
MyClass b; // Segfault if this is in.
}
As you can see from above, the code doesn't even need to get called for the application to segfault.
I'm using Netbeans 6.7.1 and GCC 4.3.2.
Now, I'm presuming there is something wrong with the linking of the library but I cannot tell what. I'm linking in other libraries (all statically linked) as well. The classes above are from the first linked library (first in the list at least). If I create an instance of a class from the second listed library, everything runs fine.
It's possible that the problem is similar (or related) to my other problem: https://stackoverflow.com/questions/1844190/linking-with-apache-xml-security-causes-unresolved-references
Does anyone have any suggestions on what might be the problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
MyClass
库中可能存在一些静态初始化错误,如果您没有源代码,将很难找到和修复。There may be some static initialization inside the the
MyClass
library that goes wrong, if you don't have the source code it will be hard to find and fix.如果您在 Linux 或 OS X 上进行开发,您可以通过在调试模式下编译并使用 valgrind 运行来获得有关此类错误的更多信息。
在调试模式下编译并不是绝对必要的,但会提供有关问题所在和位置的更好信息。
我也会在调试模式下编译包含 MyClass 的库。
另一件需要注意的事情是,该库是使用相同的编译器标志进行编译的,因为当静态对象在两种编译器设置下具有不同的内部布局时,可能会发生这种崩溃。 (当我在代码的一部分而不是另一部分使用 -DREENTRANT 编译应用程序的一部分时,我花了很长时间来跟踪这一点,第三方组件在这两种情况下最终得到了不同的布局。)
If you're developing on linux or OS X you can get a lot more information about this kind of error by compiling in debug mode and running using valgrind.
Compiling in debug mode isn't strictly necessary, but will give much better information about what is going wrong and where.
I'd compile the library containing MyClass in debug mode too.
One other thing to watch for is that the library is compiled with the same compiler flags, as this kind of crash can happen when static objects have different internal layouts, under two compiler settings. (I spent a long time tracking this down when compiling part of an application using -DREENTRANT in one part of my code and not in another, a third party component ended up with different layouts in the two cases.)
堆栈溢出。
MyClass 可能太大而无法放入堆栈。
A Stackoverflow.
MyClass might be to big to fit on the stack.
据我从代码中了解到, foobar 从未被调用过?只是声明它会导致段错误?
我可以想象,声明 MyClass 变量会导致一些模板实例化,该实例化实现了一些静态初始化,但失败了。比如说,MyClass 是从 SomeBase<> 派生的。并且无法对 SomeBase<> 内的某些静态成员进行初始化。当您删除 MyClass 变量的声明时,模板不会实例化,一切顺利......
As far, as I understand from the code, foobar is never called? Just declaring it causes segfault?
I can imagine, that declaring a MyClass variable causes some template instantiation, which implements some static initialization, which fails. Say, MyClass is derived from SomeBase<> and it is impossible to perform initialization of some static member inside SomeBase<>. When you remove the declaration of MyClass variable, template is not instantiated and everything goes well...