views:

185

answers:

4

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: http://stackoverflow.com/questions/1844190/linking-with-apache-xml-security-causes-unresolved-references

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

A: 

A Stackoverflow.

MyClass might be to big to fit on the stack.

drhirsch
Uhm, ok? How do I fix that? I'm not too sure that that's the problem, though; if I create a dummy class within the same library and try and reference it, I get the same error.
Fredrik Ullner
But he doesn't call foobar so MyClass is never placed on the stack
Andreas Brinck
If that is the case declare a pointer to MyClass and new one off the heap. If that works problem solved.
rerun
+3  A: 

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.

Andreas Brinck
I have all source code, so that should not be a problem if that is indeed the case. I will have a look.
Fredrik Ullner
Ahhhhhhhh. Found the errornous code; a call to swscanf caused things to go wrong. (I don't know what's wrong with the code but that's for a different time.)
Fredrik Ullner
+1  A: 

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

Michael Anderson
Thanks. Using Valgrind allowed me to track down the issue, in a matter of seconds. (If I could, I would have also set your response as the answer.)
Fredrik Ullner
A: 

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...
SadSido
Yes, the code is never called. Also, it doesn't matter whether I declare an instance of a class that is, essentially, empty.
Fredrik Ullner