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
15#define BOOST_ENABLE_ASSERT_HANDLER
16#include <boost/smart_ptr/enable_shared_from_raw.hpp>
17#include <boost/shared_ptr.hpp>
18#include <boost/core/lightweight_test.hpp>
19#include <stdio.h>
20
21static int assertion_failed_ = 0;
22
23namespace boost
24{
25
26void assertion_failed( char const * expr, char const * function, char const * file, long line )
27{
28 printf( format: "Assertion '%s' failed in function '%s', file '%s', line %ld\n", expr, function, file, line );
29 ++assertion_failed_;
30}
31
32} // namespace boost
33
34class X: public boost::enable_shared_from_raw
35{
36};
37
38int main()
39{
40 boost::shared_ptr<X> px;
41
42 {
43 X x;
44 px = boost::shared_from_raw( p: &x );
45 }
46
47 BOOST_TEST_EQ( assertion_failed_, 1 );
48
49 // px is a dangling pointer here
50
51 return boost::report_errors();
52}
53

source code of boost/libs/smart_ptr/test/shared_from_raw_test6.cpp