Boost logo

Boost :

From: Reece Dunn (msclrhd_at_[hidden])
Date: 2004-11-24 17:23:50


Jonathan Turkanis wrote:
> "Reece Dunn" <msclrhd_at_[hidden]> wrote in message
>>I am playing around with the library. Here are a few thoughts:
>>
>>[1] And is it possible to access Sink members from stream_facade, for
>>example:
>>
> This would require stream_facade to derive from the policy class, which I didn't
> do for several reasons. The way to access the policy is with operators * or ->:
>
> stream_facade< speech_reader > speech;
> speech->set_pitch( 50 );
> // Or: (*speech).set_pitch( 50 );

That is still just as good :).

>>[2] I would like to collapse:
>>
>> std::ofstream out( "encoded.txt" );
>> boost::io::filtering_ostream os;
>> os.push( ascii85_encoder());
>> os.push( out );
>> os << ...;
>>
>>to:
>>
>> class ascii85_ofstream: public boost::io::filtering_ostream
>> {
>> std::ofstream out;
>> public:
>> ascii85_ofstream( const char * fn ): out( fn )
>> {
>> push( ascii85_encoder());
>> push( out );
>> }
>> };
>>
>> ascii85_ofstream os( "encoded.txt" );
>> os << ...;
>
> Yes, this is certainly one of the intended uses of the library.
>
>>but this results in a crash at the destructor (tested with msvc-8.0).
>>
> I believe the problem is that the filtering_ostream (a base class) is accessing
> the ofstream (a member of a derived class) after the ofstream has been
> destroyed.

This was my thoughts as well.

> This would be unacceptable even if filtering_ostream had a
> no-close-on-exit option. I believe the solution is to pop the ofstream in the
> ascii85_ofstream destructor.
>
> ascii85_ofstream::~ascii85_ofstream()
> {
> pop(); // Or: reset();
> }
>
> Please let me know if this works.

pop() does indeed fix the problem :).

Regards,
Reece


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk