Segmentation fault using shared library?

Now that you've posted GDB output, it's clear exactly what your problem is.

Now that you've posted GDB output, it's clear exactly what your problem is. You are defining the same symbols in libokFrontPanel. So and in the libLoadLibrary.So (for lack of a better name -- it is so much easier to explain things when they are named properly), and that is causing the infinite recursion.

By default on UNIX (unlike on Windows) all global symbols from all shared libraries (and the main executable) go into single "loader symbol name space". Among other things, this means that if you define malloc in the main executable, that malloc will be called by all shared libraries, including libc (even though libc has its own malloc definition). So, here is what's happening: in libLoadLibrary.

So you defined okCUsbFrontPanel constructor. I assert that there is also a definition of that exact symbol in libokFrontPanel.so. All calls to this constructor (by default) go to the first definition (the one that the dynamic loader first observed), even though the creators of libokFrontPanel.

So did not intend for this to happen. The loop is (in the same order GDB printed them -- innermost frame on top): #1 okCUsbFrontPanel () at okFrontPanelDLL. Cpp:169 #3 okUsbFrontPanel_Construct () from libokFrontPanel.

So #2 okUsbFrontPanel_Construct () at okFrontPanelDLL. Cpp:1107 #1 okCUsbFrontPanel () at okFrontPanelDLL. Cpp:169 The call to constructor from #3 was intended to go to symbol #4 -- okCUsbFrontPanel constructor inside libokFrontPanel.so.

Instead it went to previously seen definition inside libLoadLibrary. So: you "preempted" symbol #4, and thus formed an infinite recursion loop. Moral: do not define the same symbols in multiple libraries, unless you understand the rules by which the runtime loader decides which symbol references are bound to which definitions.

EDIT: To answer 'EDIT2' of the question: Yes, the call to _ZN16okCUsbFrontPanelC1Ev from okUsbFrontPanel_Construct is going to the definition of that method inside your okFrontPanelDLL.cpp. It might be illuminating to examine objdump -d okFrontPanelDLL.o.

I have not defined any symbol: those name/classes are provided by the FPGA manufacturer. I cannot change names inside libokFrontPanel. So: should I change name to okUsbFrontPanel_Construct () inside okFrontPanelDLL.

Cpp or okCUsbFrontPanel () at okFrontPanelDLL. Cpp:169? – nick2k3 Jul 31 '09 at 10:23 Yes, I understand that libokFrontPanel.So is 3rd-party code.

You should change your code to avoid symbol name collision: change both okCUsbFrontPanel and okUsbFrontPanel_Construct. The latter change is not strictly necessary, but you should avoid all name conflicts if possible. – Employed Russian Jul 31 '09 at 17:13 Thank you very much,it worked!

One last thing that I cannot explain: this problem occurred while I was using the shared library (the one made by myself) inside a huge plugin-based program. I tried to make a simple main. Cpp which uses my shared library(prior to change symbols)and everything worked fine.. I thought that maybe this big plugin-based program uses a different way to resolve symbols than a standard program does.. any guess?

– nick2k3 Aug 1 '09 at 10:35.

Contrary to what Norman Ramsey says, a tool of choice for diagnosing segfaults is GDB, not valgrind. The latter is only useful for certain kinds of segfaults (mostly these related to heap corruption; which doesn't appear to be the case here). My crystal ball says that your dlopen() fails (you should print dlerror() if/when that happens!), and that your _okUsbFrontPanel_Construct remains NULL.

In GDB you will immediately be able to tell whether that guess is correct. My guess contradicts your statement that you "get a recursion to okUsbFrontPanel_Construct()". But just how can you know that you get such recursion, if you didn't look with GDB?

Dlopen does not fail..I do have a dlerror() print but I removed it from the code posted to short it a bit.. – nick2k3 Jul 27 '09 at 7:21 I stated that I "get a recursion to okUsbFrontPanel_Construct()" becouse if I put a printf in that function ie. "Entering etc.." I get a lot of Entering ... Entering ... – nick2k3 Jul 29 '09 at 9:43.

The tool of choice for diagnosing segfaults is valgrind. If you are misusing pointers or memory valgrind will find the problem and give you a stack trace well before the segfault occurs. On the FAQ, valgrind claims to handle shared libraries OK as long as you don't call dlclose().

If you have never used valgrind before I think you will be astonished at how easy and powerful it is. You just use 'valgrind' as the first word of your command line, and it finds your memory errors. Great stuff!

There's a short example session on Vladislav Vyshemirsky's blog.

I already heard about Valgrind but I'm in a tricky situation: my appication is itself a shared library. It is loaded by a program on which I have no control. Is there a way to attach valgrind to an already launched program?

– nick2k3 Jul 26 '09 at 1:13 As long as you can run the program from the command line, you can run it under valgrind. If the other program has no debugging symbols, the information you get from valgrind may not be very helpful, however---I have not tried using it in that mode. – Norman Ramsey Jul 26 '09 at 16:32.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions