I don't think this case needs two layers of try/catch. This thread started with a concern about throwing an exception while in a constructor. If an exception is thrown during construction, there are no pointers created. So calling "delete created pointers" is a no-op and does no harm if it is called. If an exception is thrown during loading, then "delete_created_pointers" will becalled with the desired effect. This is not to say that I know for a fact that that an exception thrown during construction can in fact create a memory leak. To be honest, I never considered that when I wrote the code so it might be possible. I would have to look into it. Robert Ramey gast128 wrote:
Robert Ramey
writes: Note: the function of 'delete_created_pointers' is to delete any pointer created during the process of loading. Construction of an archive creates no such pointers. So not calling 'delete_created_pointers' in this case will have no effect and does not need to be called.
Ok 3 things: 1) I think a throwing ctor should not create memory leaks. I believe Sutter has comments about this in his book 2) The loaded XML file can be altered by users. Therfore the load can fail and I want it to be guarded. But now I have to add a double guard:
bool Load(const std::string& cr) { std::fstream fstr(cr);
try { boost::archive::xml_iarchive ia(fstr);
try { ia >> ...; return true; } catch (boost::archive::archive_exception& re) { ia.delete_created_pointers(); } } catch (boost::archive::archive_exception& re) { }
return false; }