| 1 | #include "stdafx.h"
|
|---|
| 2 |
|
|---|
| 3 | #include <boost/test/unit_test.hpp>
|
|---|
| 4 |
|
|---|
| 5 | #include <iostream>
|
|---|
| 6 | #include <string>
|
|---|
| 7 | #include <map>
|
|---|
| 8 | #include <memory>
|
|---|
| 9 | #include <vector>
|
|---|
| 10 | #include <numeric>
|
|---|
| 11 |
|
|---|
| 12 | #include <boost/shared_ptr.hpp>
|
|---|
| 13 | #include <boost/foreach.hpp>
|
|---|
| 14 |
|
|---|
| 15 | #include <boost/archive/binary_oarchive.hpp>
|
|---|
| 16 | #include <boost/archive/binary_iarchive.hpp>
|
|---|
| 17 | #include <boost/serialization/map.hpp>
|
|---|
| 18 | #include <boost/serialization/vector.hpp>
|
|---|
| 19 | #include <boost/serialization/shared_ptr.hpp>
|
|---|
| 20 |
|
|---|
| 21 | using namespace boost;
|
|---|
| 22 | using namespace std;
|
|---|
| 23 |
|
|---|
| 24 | class ChildClass
|
|---|
| 25 | {
|
|---|
| 26 | public:
|
|---|
| 27 | friend class boost::serialization::access;
|
|---|
| 28 | template<class Archive>
|
|---|
| 29 | void serialize(Archive & ar, const unsigned int version)
|
|---|
| 30 | {
|
|---|
| 31 | // Leaks are worse if the child has data:
|
|---|
| 32 | //
|
|---|
| 33 | //ar & mData;
|
|---|
| 34 | }
|
|---|
| 35 |
|
|---|
| 36 | // Leaks are worse if the child has data:
|
|---|
| 37 | //
|
|---|
| 38 | //map<string,vector<vector<float>>> mData;
|
|---|
| 39 | };
|
|---|
| 40 |
|
|---|
| 41 | class MyClass
|
|---|
| 42 | {
|
|---|
| 43 | public:
|
|---|
| 44 | MyClass() {};
|
|---|
| 45 | ~MyClass() {};
|
|---|
| 46 |
|
|---|
| 47 | void WriteToFile(const char* file_name)
|
|---|
| 48 | {
|
|---|
| 49 | std::ofstream out_stream(file_name, ios::binary);
|
|---|
| 50 | boost::archive::binary_oarchive out_archive(out_stream);
|
|---|
| 51 | out_archive << mData;
|
|---|
| 52 | }
|
|---|
| 53 |
|
|---|
| 54 | static shared_ptr<MyClass> ReadFromFile(const char* file_name)
|
|---|
| 55 | {
|
|---|
| 56 | shared_ptr<MyClass> ret ( new MyClass() );
|
|---|
| 57 |
|
|---|
| 58 | ifstream in_stream(file_name, ios::binary);
|
|---|
| 59 | boost::archive::binary_iarchive in_archive(in_stream);
|
|---|
| 60 |
|
|---|
| 61 | in_archive >> ret->mData;
|
|---|
| 62 |
|
|---|
| 63 | return ret;
|
|---|
| 64 | }
|
|---|
| 65 |
|
|---|
| 66 | map<string, shared_ptr<ChildClass> > mData;
|
|---|
| 67 |
|
|---|
| 68 | void Print(ostream& str)
|
|---|
| 69 | {
|
|---|
| 70 | str << "MyClass @" << ((int)(void*)this) << " with " << mData.size() << " children" << endl;
|
|---|
| 71 | typedef pair<const string, shared_ptr<ChildClass>> pairt1;
|
|---|
| 72 | BOOST_FOREACH( pairt1& ii, mData )
|
|---|
| 73 | {/*
|
|---|
| 74 | str << ii.first << " => " << ii.second->mData.size() << " row(s)" << endl;
|
|---|
| 75 |
|
|---|
| 76 | typedef pair<const string, vector<vector<float>>> pairt2;
|
|---|
| 77 | BOOST_FOREACH( pairt2& jj, ii.second->mData )
|
|---|
| 78 | {
|
|---|
| 79 | str << " " << jj.first << " => [" << endl;
|
|---|
| 80 | BOOST_FOREACH( vector<float>& kk, jj.second )
|
|---|
| 81 | {
|
|---|
| 82 | str << " [";
|
|---|
| 83 | BOOST_FOREACH( float& ll, kk ) { str << ll << ", "; }
|
|---|
| 84 | str << "]" << endl;
|
|---|
| 85 | }
|
|---|
| 86 | str << " ]" << endl;
|
|---|
| 87 | }*/
|
|---|
| 88 | }
|
|---|
| 89 | }
|
|---|
| 90 | };
|
|---|
| 91 |
|
|---|
| 92 |
|
|---|
| 93 | BOOST_AUTO_TEST_CASE( boost_serialisation_mem_leak )
|
|---|
| 94 | {
|
|---|
| 95 | // create a bit of data
|
|---|
| 96 | shared_ptr<MyClass> new_item(new MyClass());
|
|---|
| 97 |
|
|---|
| 98 | // new_item must have children for the mem leak to occur
|
|---|
| 99 | new_item->mData["foo"] = shared_ptr<ChildClass>(new ChildClass());
|
|---|
| 100 |
|
|---|
| 101 | // having data in the child is not necessary for mem leak
|
|---|
| 102 | //
|
|---|
| 103 | // new_item->mData["foo"]->mData["bar"].push_back(vector<float>(2, 3.0));
|
|---|
| 104 | // new_item->mData["foo"]->mData["bar"].push_back(vector<float>(1, 7.0));
|
|---|
| 105 |
|
|---|
| 106 | new_item->Print(cout);
|
|---|
| 107 |
|
|---|
| 108 | // save & load it
|
|---|
| 109 | new_item->WriteToFile("test.bin");
|
|---|
| 110 |
|
|---|
| 111 | shared_ptr<MyClass> loaded_item = MyClass::ReadFromFile("test.bin");
|
|---|
| 112 |
|
|---|
| 113 | cout << endl << endl;
|
|---|
| 114 | loaded_item->Print(cout);
|
|---|
| 115 | }
|
|---|