segfault during __cxa_allocate_exception in SWIG wrapped library

segfault during __cxa_allocate_exception in SWIG wrapped library

While developing a SWIG wrapped C++ library for Ruby, we came across an unexplained crash during exception handling inside the C++ code.

I'm not sure of the specific circumstances to recreate the issue, but it happened first during a call to std::uncaught_exception, then after a some code changes, moved to __cxa_allocate_exception during exception construction. Neither GDB nor valgrind provided any insight into the cause of the crash.

I've found several references to similar problems, including:

The overriding theme seems to be a combination of circumstances:

  • A C application is linked to more than one C++ library
  • More than one version of libstdc++ was used during compilation
  • Generally the second version of C++ used comes from a binary-only implementation of libGL
  • The problem does not occur when linking your library with a C++ application, only with a C application

The "solution" is to explicitly link your library with libstdc++ and possibly also with libGL, forcing the order of linking.

After trying many combinations with my code, the only solution that I found that works is the LD_PRELOAD="" ruby scriptname option. That is, none of the compile-time linking solutions made any difference.

My understanding of the issue is that the C++ runtime is not being properly initialized. By forcing the order of linking you bootstrap the initialization process and it works. The problem occurs only with C applications calling C++ libraries because the C application is not itself linking to libstdc++ and is not initializing the C++ runtime. Because using SWIG (or boost::python) is a common way of calling a C++ library from a C application, that is why SWIG often comes up when researching the problem.

Is anyone out there able to give more insight into this problem? Is there an actual solution or do only workarounds exist?


Newbie: Render RGB to GTK widget — howto?


Is there any way to get the combine two xml into one xml in Linux
Following Michael Dorgan's suggestion, I'm copying my comment into an answer:. How do I know which illegal address the program access when a segmentation fault happens Found the real cause of the problem. Constructing a function call in CHopefully this will guidance any one else encountering this bug. Java socket bug on linux (0xFF sent, -3 received)You probably have any static data any where this is not being properly initialized. sendmail working but PHP mail() is failingWe did, and the quick fix was in boost-log for our code base. How do I stop/workaround Java apps stealing focus in Linux window managers Lock a mutex multiple times in the same threadThe real problem is the delay loaded library (plus statics), not the potentially multiple versions of C++ from different libraries. For more info: Since encountering this problem and its solution, I've learned this it's important to understand how statics are shared or not shared between your statically and dynamically linked libraries. On Windows this requires explicitly exporting the symbols for the shared statics (including things like singletons meant to be accessed across different libraries). The behavior is subtly different between each of the major platforms..


Having the same problem using SWIG for Python with a cpp library (Clipper), I found this using LD_PRELOAD as you suggested works for me too. As ananother workaround which doesn't require LD_PRELOAD, I found this I must also link the libstdc++ into the .so library file of my module, e.g..
ld -shared /usr/lib/i386-linux-gnu/ module.o module_wrap.o -o 
I must then import it in python without any further options..


I realise this @lefticus accepted the answer relating to what I guess amounts to undefined static init order; however, I had a very similar problem, this time with boost::python.. I tried my damndest to find any static initilisation issues and couldn't - to the point this I refactored a major chunk of our codebase; and when this didn't job ended up removing exceptions altoreceive her.. However, any more crept in and i started receive ting these segfaults again.. After any more investigation I came across this link which talks around custom allocators.. We did indeed use tcmalloc ourselves; and after I removed it from our library which is exported to boost::python i had no more issues!. So just an FYI to anyone who stumbles across this thread - if @lefticus's answer doesn't work, check if you're using a different allocator to this which python uses..


I recently ran into this problem as well. My process creates a shared object module this is used as a python C++ extension. A recent OS upgrade from RHEL 6.4 to 6.5 exposed the problem.. Following the tips here, I merely added -lstdc++ to my link switches and this solved the problem..

96 out of 100 based on 71 user ratings 506 reviews