从特定库实例化对象时出现分段错误

发布于 2024-08-13 22:38:06 字数 960 浏览 4 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

最近可好 2024-08-20 22:38:06

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.

瑾兮 2024-08-20 22:38:06

如果您在 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.)

想你的星星会说话 2024-08-20 22:38:06

堆栈溢出。

MyClass 可能太大而无法放入堆栈。

A Stackoverflow.

MyClass might be to big to fit on the stack.

洒一地阳光 2024-08-20 22:38:06

据我从代码中了解到, foobar 从未被调用过?只是声明它会导致段错误?

我可以想象,声明 MyClass 变量会导致一些模板实例化,该实例化实现了一些静态初始化,但失败了。比如说,MyClass 是从 SomeBase<> 派生的。并且无法对 SomeBase<> 内的某些静态成员进行初始化。当您删除 MyClass 变量的声明时,模板不会实例化,一切顺利......

class MyClass : public SomeBase<MyClass>{};

template<typename TYPE>
class SomeBase<TYPE> { static CauseASegfault* m_casf; };

// some bad m_casf initialization here...

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...

class MyClass : public SomeBase<MyClass>{};

template<typename TYPE>
class SomeBase<TYPE> { static CauseASegfault* m_casf; };

// some bad m_casf initialization here...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文