In my code, I have to keep a persistent boost::oarchive object to write. So I have a writer class which keeps pointers of ostream and boost::oarchive pointers say -
ofstream *fs = new ofstream();
boost::archive::binary_oarchive *afs = new boost::archive::binary_oarchive( *fs );
I am having issues while destructing them. Here is what I do in my clear function to release the objects -
delete fs;
delete afs;
#valigrind throws invalid read
Valgrind throws an invalid read error while destructing 'afs' here - is 'delete fs' not required? Does deleting the boost object sufficient to release ofstream object as well?
The archive probably wants to flush something to file stream before self destruction, but you already closed the file stream.
So, delete archive first, then delete your ofstream.
In most cases, it is safe to destruct dependent objects in the reverse order of their construction.