English 中文(简体)
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!
";

 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?

最佳回答

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




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签