From 8d6517484cd542cc4c634a8d533557b843f37a47 Mon Sep 17 00:00:00 2001 From: Peter Dimov Date: Fri, 22 Nov 2002 13:49:54 +0000 Subject: [PATCH] Stricter tests; associated bug fixes. ;-) [SVN r16369] --- include/boost/shared_ptr.hpp | 6 ++++-- include/boost/weak_ptr.hpp | 7 ++++++- shared_ptr_test.cpp | 9 ++++++++- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/include/boost/shared_ptr.hpp b/include/boost/shared_ptr.hpp index cbd9fd3..5a9a44b 100644 --- a/include/boost/shared_ptr.hpp +++ b/include/boost/shared_ptr.hpp @@ -140,8 +140,10 @@ public: #endif template - explicit shared_ptr(weak_ptr const & r): px(r.px), pn(r.pn) // may throw + explicit shared_ptr(weak_ptr const & r): pn(r.pn) // may throw { + // it is now safe to copy r.px, as pn(r.pn) did not throw + px = r.px; } template @@ -166,7 +168,7 @@ public: template shared_ptr(shared_ptr const & r, detail::polymorphic_cast_tag): px(dynamic_cast(r.px)), pn(r.pn) { - if (px == 0) + if(px == 0) { boost::throw_exception(std::bad_cast()); } diff --git a/include/boost/weak_ptr.hpp b/include/boost/weak_ptr.hpp index a4154ea..d08fa5d 100644 --- a/include/boost/weak_ptr.hpp +++ b/include/boost/weak_ptr.hpp @@ -16,6 +16,8 @@ #include +#include // for std::memcpy + #ifdef BOOST_MSVC // moved here to work around VC++ compiler crash # pragma warning(push) # pragma warning(disable:4284) // odd return type for operator-> @@ -42,8 +44,11 @@ public: // generated copy constructor, assignment, destructor are fine template - weak_ptr(weak_ptr const & r): px(r.px), pn(r.pn) // never throws + weak_ptr(weak_ptr const & r): pn(r.pn) // never throws { + // r.px may be an invalid pointer value + using namespace std; + memcpy(&this->px, &r.px, sizeof(r.px)); } template diff --git a/shared_ptr_test.cpp b/shared_ptr_test.cpp index 87553d5..cbaaeed 100644 --- a/shared_ptr_test.cpp +++ b/shared_ptr_test.cpp @@ -99,7 +99,9 @@ private: long X::instances = 0; -struct Y: public A, public X +// virtual inheritance stresses the implementation + +struct Y: public A, public virtual X { static long instances; @@ -771,6 +773,11 @@ void weak_ptr_constructor() catch(boost::bad_weak_ptr) { } + + { + boost::weak_ptr wp2(wp); + boost::weak_ptr wp3(wp); + } } void auto_ptr_constructor()