Heap vs. Stack allocation of filtering_streambuf??
Hi Folks - I'm working on a mod to an existing program to write compressed output files. The following code works fine, but if I put the indicated code in a function, it drops core when I attempt to write to the ofile ostream - presumably because the filtering_streambuf<output> out was allocated on the stack and no longer exists after the function exits. #include <fstream> #include <iostream> #include <string> #include <boost/iostreams/filtering_streambuf.hpp> #include <boost/iostreams/filter/bzip2.hpp> using namespace std; using namespace boost::iostreams; using namespace boost::iostreams::bzip2; int main() { string x; fstream infile; infile.open("00011.eph.org", ios::in); ostream *ofile; //******* To be put in function ************************ fstream *file2; file2 = new fstream; file2->open("00011.eph.org.bz2", ios_base::out | ios_base::binary); filtering_streambuf<output> out; out.push(bzip2_compressor()); out.push(*file2); ofile = new ostream(&out); //******* To be put in function ************************ while(!infile.eof()) { getline(infile, x); *ofile << x << endl; } return 0; } //***************************************************************************** Now if I attempt to dynamically allocate the filtering_streambuf. #include <fstream> #include <iostream> #include <string> #include <boost/iostreams/filtering_streambuf.hpp> #include <boost/iostreams/filter/bzip2.hpp> using namespace std; using namespace boost::iostreams; using namespace boost::iostreams::bzip2; int main() { string x; fstream infile; infile.open("00011.eph.org", ios::in); ostream *ofile; //******* To be put in function ************************ fstream *file2; file2 = new fstream; file2->open("00011.eph.org.bz2", ios_base::out | ios_base::binary); filtering_streambuf<output> *out; out = new filtering_streambuf<output>; out->push(bzip2_compressor()); out->push(*file2); ofile = new ostream(out); //******* To be put in function ************************ while(!infile.eof()) { getline(infile, x); *ofile << x << endl; } return 0; } This code compiles and runs fine, but the compressed output file, 00011.eph.org.bz2, ends up empty. Moving the indicated code to a function again works fine - but again, no output?? The reasoning for all this is that I'd like to be able to open and set up the various output streams in a function, and then pass those streams around as needed. Which means they need to be dynamically allocated/built on the heap. But it's not working?? Ideas? What am I missing here? Thanks Eric
On Thu, Nov 19, 2009 at 10:44:52AM -0700, Eric R George <Eric.R.George@aero.org> was heard to say:
This code compiles and runs fine, but the compressed output file, 00011.eph.org.bz2, ends up empty. Moving the indicated code to a function again works fine - but again, no output??
I'm not an iostreams expert, but I bet you need to flush and/or close your stream. If you were deleting it (something you also want to do) that would probably do it for you. Daniel
Yea, turned out to be something like that. I had tried flushing the ostream (ofile) and the fstream (file2),and had tried deleting them also. But I needed to also flush the filtering_streambuf (out), Thanks for the help, Eric Daniel Burrows <dburrows@algebraicthunk.net> Sent by: boost-users-bounces@lists.boost.org 11/19/2009 12:23 PM Please respond to boost-users@lists.boost.org To boost-users@lists.boost.org cc Subject Re: [Boost-users] Heap vs. Stack allocation of filtering_streambuf?? On Thu, Nov 19, 2009 at 10:44:52AM -0700, Eric R George <Eric.R.George@aero.org> was heard to say:
This code compiles and runs fine, but the compressed output file, 00011.eph.org.bz2, ends up empty. Moving the indicated code to a function again works fine - but again, no output??
I'm not an iostreams expert, but I bet you need to flush and/or close your stream. If you were deleting it (something you also want to do) that would probably do it for you. Daniel _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
participants (2)
-
Daniel Burrows
-
Eric R George