1 | /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8 |
---|
2 | // test_delete_pointer.cpp |
---|
3 | |
---|
4 | // (C) Copyright 2002 Vahan Margaryan. |
---|
5 | // Use, modification and distribution is subject to the Boost Software |
---|
6 | // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at |
---|
7 | // http://www.boost.org/LICENSE_1_0.txt) |
---|
8 | // |
---|
9 | |
---|
10 | #include <fstream> |
---|
11 | |
---|
12 | #include <cstdio> // remove |
---|
13 | #include <boost/config.hpp> |
---|
14 | #if defined(BOOST_NO_STDC_NAMESPACE) |
---|
15 | namespace std{ |
---|
16 | using ::remove; |
---|
17 | } |
---|
18 | #endif |
---|
19 | |
---|
20 | #include "test_tools.hpp" |
---|
21 | #include <boost/preprocessor/stringize.hpp> |
---|
22 | #include BOOST_PP_STRINGIZE(BOOST_ARCHIVE_TEST) |
---|
23 | #include <boost/detail/no_exceptions_support.hpp> |
---|
24 | |
---|
25 | #include <boost/serialization/nvp.hpp> |
---|
26 | #include <boost/serialization/split_member.hpp> |
---|
27 | #include <boost/serialization/vector.hpp> |
---|
28 | |
---|
29 | //A holds a pointer to another A, but doesn't own the pointer. |
---|
30 | //objCount |
---|
31 | class A |
---|
32 | { |
---|
33 | friend class boost::serialization::access; |
---|
34 | template<class Archive> |
---|
35 | void save(Archive &ar, const unsigned int /* file_version */) const |
---|
36 | { |
---|
37 | ar << BOOST_SERIALIZATION_NVP(next_); |
---|
38 | } |
---|
39 | template<class Archive> |
---|
40 | void load(Archive & ar, const unsigned int /* file_version */) |
---|
41 | { |
---|
42 | static int i = 0; |
---|
43 | ++i; |
---|
44 | bool b = false; |
---|
45 | if(i == 2) |
---|
46 | b = true; |
---|
47 | |
---|
48 | ar >> BOOST_SERIALIZATION_NVP(next_); |
---|
49 | if(b) |
---|
50 | boost::throw_exception(0); |
---|
51 | } |
---|
52 | BOOST_SERIALIZATION_SPLIT_MEMBER() |
---|
53 | public: |
---|
54 | A() |
---|
55 | { |
---|
56 | next_ = 0; |
---|
57 | ++objcount; |
---|
58 | } |
---|
59 | A(const A& a) |
---|
60 | { |
---|
61 | next_ = a.next_; ++objcount; |
---|
62 | } |
---|
63 | ~A() |
---|
64 | { |
---|
65 | --objcount; |
---|
66 | } |
---|
67 | A* next_; |
---|
68 | static int objcount; |
---|
69 | }; |
---|
70 | |
---|
71 | |
---|
72 | int A::objcount = 0; |
---|
73 | |
---|
74 | int |
---|
75 | test_main( int /* argc */, char* /* argv */[] ) |
---|
76 | { |
---|
77 | std::vector<A*> vec; |
---|
78 | A* a = new A; |
---|
79 | a->next_ = 0; |
---|
80 | vec.push_back(a); |
---|
81 | |
---|
82 | //fill the vector with chained A's. The vector is assumed |
---|
83 | //to own the objects - we will destroy the objects through this vector. |
---|
84 | unsigned int i; |
---|
85 | for(i = 1; i < 10; ++i) |
---|
86 | { |
---|
87 | a = new A; |
---|
88 | vec[i - 1]->next_ = a; |
---|
89 | a->next_ = 0; |
---|
90 | vec.push_back(a); |
---|
91 | } |
---|
92 | |
---|
93 | const char * testfile = boost::archive::tmpnam(NULL); |
---|
94 | BOOST_REQUIRE(NULL != testfile); |
---|
95 | |
---|
96 | //output the vector |
---|
97 | { |
---|
98 | test_ostream os(testfile, TEST_STREAM_FLAGS); |
---|
99 | test_oarchive oa(os); |
---|
100 | oa << BOOST_SERIALIZATION_NVP(vec); |
---|
101 | } |
---|
102 | |
---|
103 | //erase the objects |
---|
104 | for(i = 0; i < vec.size(); ++i) |
---|
105 | delete vec[i]; |
---|
106 | vec.clear(); |
---|
107 | |
---|
108 | //read the vector back |
---|
109 | { |
---|
110 | test_istream is(testfile, TEST_STREAM_FLAGS); |
---|
111 | test_iarchive ia(is); |
---|
112 | BOOST_TRY { |
---|
113 | ia >> BOOST_SERIALIZATION_NVP(vec); |
---|
114 | } |
---|
115 | BOOST_CATCH (...){ |
---|
116 | ia.delete_created_pointers(); |
---|
117 | vec.clear(); |
---|
118 | } |
---|
119 | BOOST_CATCH_END |
---|
120 | } |
---|
121 | |
---|
122 | //delete the objects |
---|
123 | for(i = 0; i < vec.size(); ++i) |
---|
124 | delete vec[i]; |
---|
125 | vec.clear(); |
---|
126 | |
---|
127 | //identify the leaks |
---|
128 | BOOST_CHECK(A::objcount == 0); |
---|
129 | std::remove(testfile); |
---|
130 | return EXIT_SUCCESS; |
---|
131 | } |
---|
132 | |
---|
133 | |
---|