*When* does a C++ exception blow up C code -
ok, need -fexceptions allow c++ exception propagate through c code. tried compare results both c++ , c, and, can tell, compiled routines identical @ assembly, without option [1]. here test:
#include <unistd.h> typedef int (*callback)(void* param,void* buffer,int n); void write_wrapper(int fd,const void* buffer,int n) { const char* temp=(const char*)buffer; while(n!=0) { int k=write(fd,temp,n); n-=k; temp+=k; } } int test(callback cb,void* cb_param) { char buffer[1024]; int n=0; { n=cb(cb_param,buffer,1024); write_wrapper(stdout_fileno,buffer,n); } while(n!=1024); } int test2(callback cb,void* cb_param) { char more_stack_space_please[1024]={0}; cb(cb_param,more_stack_space_please,1024); write_wrapper(stdout_fileno,more_stack_space_please,1024); test(cb,cb_param); } here, caller free resources, there should no leak though callback function throws exception.
even though example appears work (tested simple c++ driver)
#include "lib.h" #include <cstdio> class resource { public: resource() {fprintf(stderr,"a resource\n");} ~resource() {fprintf(stderr,"not resource\n");} }; int main() { try { resource foo; test([](void* cb_param,void* buffer, int n)->int { resource bar; throw "test"; },nullptr); test2([](void* cb_param,void* buffer, int n)->int { throw "test2"; },nullptr); } catch(const char* err) { fprintf(stderr,"error: %s\n",err); return -1; } return 0; } , have got uncaught exception errors gtk callbacks. thoughts:
- gtk shared library, maybe affects linker, , not code generator
- gtk (and glib) contains lot strange hacks, may screw things up
the problem question, asks defined behavior undefined result.
the c standard not describe behavior when exception thrown through c function, because shouldn't happen.
the c++ standard not explain mechanism uncaught exception dropping through c function because shouldn't happen.
a practical example
windows visual c++ uses fs: segment register thread local storage, , particular slot in thread-local data segment create linked list of catch frames.
when c++ exception thrown, linked list inspected destructors of stack objects, , suitable catch frame.
the c compiler may not aware of c++ usage of these resources, , able re-use slots different purpose. if slots used incompatible function, crash happen.
a particular platform , compiler may support this, @ whim of platform such.
gcc c compiler, not throw exceptions. can create code exception aware supports -fexception.
gnu compiler : using exceptions suggests compiling c code -fexceptions , says
in particular, unwinding frame no exception handling data cause runtime abort.
although context of statement not describe whether due being debugged clause, or due exception mechanism identifying illegal state , causing abort.
the actual implementation hardware , os specific, not specified in question, , specific answer, these should specified.
- c++ not have compatibility abi. calling through c++ objects compiled different compilers not give guarantee of working.
- exceptions thrown between different modules not recommended, implementation on compiler has produced code not intra-version compatible.
- assuming have control on both c , c++ code, ensure c safe enabling exceptions , possibly compiling c c++ (with
extern "c"wrapper, ensuring c++ compilation unit.
Comments
Post a Comment