shared_from_raw_test6.cpp 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. //
  2. // shared_from_raw_test6
  3. //
  4. // Tests that dangling shared_ptr instances are caught by
  5. // the BOOST_ASSERT in ~enable_shared_from_raw
  6. //
  7. // Copyright 2014 Peter Dimov
  8. //
  9. // Distributed under the Boost Software License, Version 1.0.
  10. //
  11. // See accompanying file LICENSE_1_0.txt or copy at
  12. // http://www.boost.org/LICENSE_1_0.txt
  13. //
  14. #define BOOST_ENABLE_ASSERT_HANDLER
  15. #include <boost/smart_ptr/enable_shared_from_raw.hpp>
  16. #include <boost/shared_ptr.hpp>
  17. #include <boost/core/lightweight_test.hpp>
  18. #include <stdio.h>
  19. static int assertion_failed_ = 0;
  20. namespace boost
  21. {
  22. void assertion_failed( char const * expr, char const * function, char const * file, long line )
  23. {
  24. printf( "Assertion '%s' failed in function '%s', file '%s', line %ld\n", expr, function, file, line );
  25. ++assertion_failed_;
  26. }
  27. } // namespace boost
  28. class X: public boost::enable_shared_from_raw
  29. {
  30. };
  31. int main()
  32. {
  33. boost::shared_ptr<X> px;
  34. {
  35. X x;
  36. px = boost::shared_from_raw( &x );
  37. }
  38. BOOST_TEST_EQ( assertion_failed_, 1 );
  39. // px is a dangling pointer here
  40. return boost::report_errors();
  41. }