|
Boost Users : |
From: Jeff (grahamspam_at_[hidden])
Date: 2007-05-03 20:46:35
Hello,
The code below works fine in 1_33, but throws a bad_alloc exception
after some 10-20K iterations in 1_34_0.
Am I doing something wrong? Did the API or usage requirements change?
Thanks,
Jeff
---------------------------------------------------------------------------------------------------------------------------------------
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <boost/iostreams/filtering_stream.hpp>
#include <boost/iostreams/copy.hpp>
#include <boost/iostreams/filter/zlib.hpp>
#include <boost/iostreams/device/array.hpp>
#include <boost/iostreams/stream.hpp>
#include <boost/iostreams/filtering_streambuf.hpp>
bool binaryFile2String(const std::string& filename, std::string& out)
{
std::ifstream is(filename.c_str(),
std::ios_base::in|std::ios_base::binary);
if ( is.bad() || is.fail() )
return false;
is.seekg(0,std::ios::end);
const std::ios::pos_type len(is.tellg());
if ( len > out.max_size() )
return false;
out.reserve(len);
is.seekg(0, std::ios::beg);
std::ostringstream os;
os << is.rdbuf();
if ( ! is && ! is.eof() )
return false;
out = os.str();
is.close();
return true;
}
int main(int argc, char** argv)
{
const char* inFileName = "/tmp/car.xml"; // put your data file here
int loops(20000);
if ( argc > 1 )
loops = atoi(argv[1]);
std::string stringData;
if ( ! binaryFile2String(inFileName,stringData) )
{
std::cerr<<"Failed reading file "<<inFileName<<" into
string"<<std::endl;
exit(1);
}
register int i(0);
try
{
for (i=loops; i > 0; --i)
{
if ( 0 == (i % 1000) )
std::cerr<<' '<<i;
std::istringstream ifs(stringData);
std::ostringstream toFS;
boost::iostreams::filtering_ostream ofs;
// this line causing the bad_alloc exception
ofs.push(boost::iostreams::zlib_compressor());
ofs.push(toFS);
boost::iostreams::copy(ifs,ofs);
ofs.reset();
}
}
catch(const std::exception& e)
{
std::cerr<<"\nCaught std::exception on iteration "<<i<<":
"<<e.what()<<std::endl;
}
if ( 0 == i )
{
std::cerr<<"\nTEST OK on "<<loops<<" iterations"<<std::endl;
}
else
{
std::cerr<<"\nTEST FAILED on iteration "<<i<<std::endl;
}
return 0;
}
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net