开发者

Segmentation fault when instantiating object from particular library

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/link开发者_JAVA技巧ing-with-apache-xml-security-causes-unresolved-references

Does anyone have any suggestions on what might be the problem?


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.


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


A Stackoverflow.

MyClass might be to big to fit on the stack.


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜