mirror of
https://github.com/boostorg/smart_ptr.git
synced 2025-07-31 13:17:23 +02:00
Fixed the previous weak_ptr 'fix'.
[SVN r16370]
This commit is contained in:
@ -16,8 +16,6 @@
|
|||||||
|
|
||||||
#include <boost/shared_ptr.hpp>
|
#include <boost/shared_ptr.hpp>
|
||||||
|
|
||||||
#include <cstring> // for std::memcpy
|
|
||||||
|
|
||||||
#ifdef BOOST_MSVC // moved here to work around VC++ compiler crash
|
#ifdef BOOST_MSVC // moved here to work around VC++ compiler crash
|
||||||
# pragma warning(push)
|
# pragma warning(push)
|
||||||
# pragma warning(disable:4284) // odd return type for operator->
|
# pragma warning(disable:4284) // odd return type for operator->
|
||||||
@ -26,6 +24,8 @@
|
|||||||
namespace boost
|
namespace boost
|
||||||
{
|
{
|
||||||
|
|
||||||
|
template<class T> shared_ptr<T> make_shared(weak_ptr<T> const & r); // never throws
|
||||||
|
|
||||||
template<class T> class weak_ptr
|
template<class T> class weak_ptr
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
@ -43,12 +43,39 @@ public:
|
|||||||
|
|
||||||
// generated copy constructor, assignment, destructor are fine
|
// generated copy constructor, assignment, destructor are fine
|
||||||
|
|
||||||
|
|
||||||
|
//
|
||||||
|
// The simple implementation of the converting constructor:
|
||||||
|
//
|
||||||
|
// template<class Y>
|
||||||
|
// weak_ptr(weak_ptr<Y> const & r): px(r.px), pn(r.pn) // never throws
|
||||||
|
// {
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// has a serious problem.
|
||||||
|
//
|
||||||
|
// r.px may already have been invalidated. The px(r.px)
|
||||||
|
// conversion may require access to *r.px (virtual inheritance).
|
||||||
|
//
|
||||||
|
// It is not possible to avoid spurious access violations since
|
||||||
|
// in multithreaded programs r.px may be invalidated at any point.
|
||||||
|
//
|
||||||
|
// A safe conversion between weak_ptr<T> and weak_ptr<Y> must involve
|
||||||
|
// a temporary shared_ptr.
|
||||||
|
//
|
||||||
|
// It is not yet clear whether the converting constructor should be
|
||||||
|
// present at all, or it's better to require an explicit make_shared call.
|
||||||
|
//
|
||||||
|
// The only case for the converting constructor is the weak_ptr<T> -> weak_ptr<cv void>
|
||||||
|
// conversion.
|
||||||
|
//
|
||||||
|
|
||||||
template<class Y>
|
template<class Y>
|
||||||
weak_ptr(weak_ptr<Y> const & r): pn(r.pn) // never throws
|
weak_ptr(weak_ptr<Y> const & r) // never throws
|
||||||
{
|
{
|
||||||
// r.px may be an invalid pointer value
|
shared_ptr<Y> tmp = make_shared(r);
|
||||||
using namespace std;
|
px = tmp.px;
|
||||||
memcpy(&this->px, &r.px, sizeof(r.px));
|
pn = tmp.pn;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class Y>
|
template<class Y>
|
||||||
@ -58,14 +85,6 @@ public:
|
|||||||
|
|
||||||
#if !defined(BOOST_MSVC) || (BOOST_MSVC > 1200)
|
#if !defined(BOOST_MSVC) || (BOOST_MSVC > 1200)
|
||||||
|
|
||||||
template<class Y>
|
|
||||||
weak_ptr & operator=(weak_ptr<Y> const & r) // never throws
|
|
||||||
{
|
|
||||||
px = r.px;
|
|
||||||
pn = r.pn;
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
template<class Y>
|
template<class Y>
|
||||||
weak_ptr & operator=(shared_ptr<Y> const & r) // never throws
|
weak_ptr & operator=(shared_ptr<Y> const & r) // never throws
|
||||||
{
|
{
|
||||||
|
Reference in New Issue
Block a user