|
Boost Users : |
From: Ying (ying_at_[hidden])
Date: 2007-07-06 06:04:46
The attached code works fine with version 1.33.1 but generates memory
leaks with 1.34.0.
Not sure it's a bug in Serialization or Iostreams.
Compiler: Visual Studio 2005 SP1
Thanks,
Ying
http://www.lw-works.com - Clipboard Recorder
#include <cstdlib>
//#include <boost/array.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
//#include <boost/serialization/deque.hpp>
#include <boost/iostreams/categories.hpp>
#include <boost/iostreams/filter/zlib.hpp>
#include <boost/iostreams/filtering_stream.hpp>
#include <boost/iostreams/concepts.hpp> // sink, source
#include <boost/iostreams/stream.hpp>
#include <vector>
#if _MSC_VER
# ifdef _DEBUG
# include <crtdbg.h>
# ifndef new
# define new new(_NORMAL_BLOCK, __FILE__, __LINE__)
# endif
# ifndef malloc
# define malloc(size) _malloc_dbg(size, _NORMAL_BLOCK, __FILE__, __LINE__)
# endif
# endif
#endif
namespace cmn{ namespace iostreams{
template<typename Container>
class container_sink {
public:
typedef typename Container::value_type char_type;
typedef boost::iostreams::sink_tag category;
container_sink(Container& container) : container_(container) { }
std::streamsize write(const char_type* s, std::streamsize n)
{
container_.insert(container_.end(), s, s + n);
return n;
}
Container& container() { return container_; }
private:
Container& container_;
}; // class container_sink
}} // namespace cmn::iostreams
namespace cmn{ namespace iostreams{
template<typename Container>
class container_source {
public:
typedef typename Container::value_type char_type;
typedef boost::iostreams::source_tag category;
container_source(Container& container)
: container_(container), pos_(0)
{ }
std::streamsize read(char_type* s, std::streamsize n)
{
using namespace std;
streamsize amt = static_cast<streamsize>(container_.size() - pos_);
streamsize result = (min)(n, amt);
if (result != 0) {
std::copy( container_.begin() + pos_,
container_.begin() + pos_ + result,
s );
pos_ += result;
return result;
} else {
return -1; // EOF
}
}
Container& container() { return container_; }
private:
typedef typename Container::size_type size_type;
Container& container_;
size_type pos_;
}; // class container_source
}} // namespace cmn::iostreams
namespace cmn{
template<class T, class Container> bool compress_object(Container& v, const T& o)
{
typedef cmn::iostreams::container_sink<Container> sink;
boost::iostreams::stream<sink> outs(v);
boost::iostreams::filtering_stream<boost::iostreams::output> fs;
fs.push(boost::iostreams::zlib_compressor());
fs.push(outs);
boost::archive::text_oarchive oa(fs);
oa << o;
return true;
}
template<class T, class Container> bool decompress_object(const Container& v, T& o)
{
//ASSERT(v.size());
if(!v.size())
return false;
typedef cmn::iostreams::container_source<const Container> source;
boost::iostreams::stream<source> ins(v);
boost::iostreams::filtering_stream<boost::iostreams::input> fs;
fs.push(boost::iostreams::zlib_decompressor());
fs.push(ins);
boost::archive::text_iarchive ia(fs);
ia >> o;
return true;
}
} // namespace cmn
int main()
{
_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | /*_CRTDBG_CHECK_ALWAYS_DF | _CRTDBG_CHECK_CRT_DF | */_CRTDBG_LEAK_CHECK_DF);
std::string data;
std::vector<char> buf;
cmn::compress_object(buf, data);
cmn::decompress_object(buf, data);
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