vendredi 27 novembre 2015

How do I try/catch an object creation in C++?

I'm running my program in catchsegv and it shows boost::archive::text_oarchive destructor running just before my program segfaults:

Backtrace:
??:?(_ZN5boost7archive28basic_streambuf_locale_saverIcSt11char_traitsIcEED1Ev)[0x50c678]
??:?(_ZN5boost7archive21basic_text_oprimitiveISoED1Ev)[0x50c833]
/usr/local/include/boost/archive/text_oarchive.hpp:97(_ZN5boost7archive18text_oarchive_implINS0_13text_oarchiveEED2Ev)[0x417baf]
/usr/local/include/boost/archive/text_oarchive.hpp:114(_ZN5boost7archive13text_oarchiveD2Ev)[0x416645]
...

I'm not sure why the boost::archive::text_oarchive destructor is running, as far as I can tell the object isn't finished with. So my guess is that an exception has been thrown and that's why the destructor is running..?

I have this code:

std::stringstream ss;
boost::archive::text_oarchive outArchive(ss);
outArchive << *instPtr;

The catchsegv output shows the text_oarchive destructor is called on the middle line, my program never reaches the third line.

So what I want to do is wrap the outArchive construction in a try/catch block. I tried like this:

boost::archive::text_oarchive outArchive;
try {
  boost::archive::text_oarchive outArchiveTemp(ss);
  outArchive = outArchiveTemp;
}
catch ( std::exception& ex ) {
  BOOST_LOG_TRIVIAL(info) << "Error creating text_oarchive...";
  exit( 1 );
}

But that doesn't work. I'm afraid I don't understand C++ well enough to do what I want to do, which is basically to try/catch object creation in order to diagnose why the outArchive destructor is running and my code segfaulting... help?

Aucun commentaire:

Enregistrer un commentaire