test_delete_pointer.cpp 2.8 KB

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