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="libGL.so libstdc++.so.6" 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?
1:Is there any way to get the combine two xml into one xml in Linux
I must then import it in python without any further options..
ld -shared /usr/lib/i386-linux-gnu/libstdc++.so.6 module.o module_wrap.o -o _module.so
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
tcmallocourselves; and after I removed it from our library which is exported to
boost::pythoni 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