Preventing iostreams from accessing freed memory

Hi all, I'm unable to prevent Boost iostreams from accessing memory once it has been freed, and I'm hoping someone here familiar with iostreams has some advice. As far as I can tell, when I push a standard iostream onto a filter, the filter holds on to a reference of it. If this happens inside a function, then when the function returns the iostream is freed, followed by the filter. In its destructor the filter tries to perform a flush/close and promptly segfaults, as it is trying to access the iostream which has just been freed. There is a set_auto_close() function I can call on the filter which sounds like it would do the trick, but it doesn't and according to the docs it's because it "does not prevent the Filters and Devices in the chain from being closed [flushed] automatically if the chain is complete at destruction." So is there any way I can prevent a filtering_stream from performing I/O in its destructor? Thanks, Adam.

So is there any way I can prevent a filtering_stream from performing I/O in its destructor?
Just for the record I think I found a solution. If I create my own class inheriting from filtering_stream and in its destructor call this->reset(), and only create instances of this class instead, then it flushes/closes everything on destruct (while all the streams are valid) instead of after it (when the streams have been freed.) So far this has stopped all the segfaults! Cheers, Adam.
participants (1)
-
Adam Nielsen