title: *When* does a C++ exception blow up C code
body: <p>Ok, so I need <code>-fexceptions</code> to allow a C++ exception to propagate through C code. I tried to compare the results from both C++ and C, and, from what I can tell, the compiled routines are <em>identical</em> at the assembly, even without that option [1]. Here is the test:</p>
  
  <pre><code>#include &lt;unistd.h&gt;
  
  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;
      do
          {
          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);
  }
  </code></pre>
  
  <p>Here, it is up to the caller to free resources, so there should be no leak even though the callback function throws an exception.</p>
  
  <p>Even though this example appears to work (tested with simple C++ driver)</p>
  
  <pre><code>#include "lib.h"
  #include &lt;cstdio&gt;
  
  class Resource
      {
      public:
          Resource()
              {fprintf(stderr,"A Resource\n");}
  
          ~Resource()
              {fprintf(stderr,"Not a Resource\n");}
      };
  
  int main()
      {
      try
          {
          Resource foo;
          test([](void* cb_param,void* buffer, int n)-&gt;int
              {
              Resource bar;
              throw "test";
              },nullptr);
          test2([](void* cb_param,void* buffer, int n)-&gt;int
              {
              throw "test2";
              },nullptr);
          }
      catch(const char* err)
          {
          fprintf(stderr,"Error: %s\n",err);
          return -1;
          }
      return 0;
      }
  </code></pre>
  
  <p>, I have got uncaught exception errors from GTK callbacks. My thoughts:</p>
  
  <ul>
  <li>GTK is a shared library, so maybe it only affects the linker, and not the code generator</li>
  <li>GTK (and GLib) contains a lot strange hacks, that may screw things up</li>
  </ul>
  
  <p>[1] <a href="https://godbolt.org/g/HfSnjB" rel="nofollow noreferrer">https://godbolt.org/g/HfSnjB</a></p>