My C++ software often enables exceptions on output streams (via a call to 
std::ostream::exceptions), rather than relying on checking that the stream 
is good afterward. However, this interacts very poorly with 
SerializeToOstream. If SerializeToOstream encounters an exception while 
writing, it starts unwinding the stack (as expected), but this calls 
CopyingOutputStreamAdaptor's destructor, which tries to flush any buffered 
data, which throws another exception before the first one could be handled, 
which terminates the program.

Is there a good approach to handling this, other than disabling exceptions 
on any ostreams I'm using with Protocol Buffers? (Ideally, protobuf 
wouldn't do something that could throw exceptions from destructors, but I 
suspect that this would be a nontrivial change. I noticed that Cap'n Proto 
<https://capnproto.org/cxx.html> uses std::uncaught_exceptions to ensure 
that something like this happen, but I know Google doesn't use exceptions 
in their own code, so it's a non-issue there.)

Here's the call stack on my Ubuntu 14.04 machine, using Protocol Buffers 
2.5.0:

#0  0xb7fdd428 in __kernel_vsyscall ()
#1  0xb7c20607 in __GI_raise (sig=sig@entry=6)
    at ../nptl/sysdeps/unix/sysv/linux/raise.c:56
#2  0xb7c23a33 in __GI_abort () at abort.c:89
#3  0xb7e12b35 in __gnu_cxx::__verbose_terminate_handler() ()
   from /usr/lib/i386-linux-gnu/libstdc++.so.6
#4  0xb7e10853 in ?? () from /usr/lib/i386-linux-gnu/libstdc++.so.6
#5  0xb7e0f6b9 in ?? () from /usr/lib/i386-linux-gnu/libstdc++.so.6
#6  0xb7e0fe2e in __gxx_personality_v0 ()
   from /usr/lib/i386-linux-gnu/libstdc++.so.6
#7  0xb7db4d5f in ?? () from /lib/i386-linux-gnu/libgcc_s.so.1
#8  0xb7db5207 in _Unwind_Resume () from /lib/i386-linux-gnu/libgcc_s.so.1
#9  0xb7f10cbc in 
google::protobuf::io::CopyingOutputStreamAdaptor::~CopyingOutputStreamAdaptor() 
() from /usr/lib/i386-linux-gnu/libprotobuf.so.8
#10 0xb7f825b9 in 
google::protobuf::io::OstreamOutputStream::~OstreamOutputStream() () from 
/usr/lib/i386-linux-gnu/libprotobuf.so.8
#11 0xb7f65629 in 
google::protobuf::Message::SerializeToOstream(std::ostream*) const () from 
/usr/lib/i386-linux-gnu/libprotobuf.so.8
#12 0x0804ad6d in main ()

Josh Kelley

-- 
You received this message because you are subscribed to the Google Groups 
"Protocol Buffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/protobuf.
For more options, visit https://groups.google.com/d/optout.

Reply via email to