| Index: base/memory/weak_ptr.h
|
| diff --git a/base/memory/weak_ptr.h b/base/memory/weak_ptr.h
|
| index 8a433922fdf0009f1bc4ed0a1a0caa146a798977..dec26b931fa6a760bbf0cbd20af86e09f87ad3bb 100644
|
| --- a/base/memory/weak_ptr.h
|
| +++ b/base/memory/weak_ptr.h
|
| @@ -86,7 +86,7 @@ class BASE_EXPORT WeakReference {
|
| // deleted from another via base::WeakPtr::~WeakPtr().
|
| class BASE_EXPORT Flag : public RefCountedThreadSafe<Flag> {
|
| public:
|
| - Flag();
|
| + Flag(bool bound_to_constructor_sequence);
|
|
|
| void Invalidate();
|
| bool IsValid() const;
|
| @@ -112,7 +112,7 @@ class BASE_EXPORT WeakReference {
|
|
|
| class BASE_EXPORT WeakReferenceOwner {
|
| public:
|
| - WeakReferenceOwner();
|
| + WeakReferenceOwner(bool bound_to_constructor_sequence);
|
| ~WeakReferenceOwner();
|
|
|
| WeakReference GetRef() const;
|
| @@ -124,6 +124,7 @@ class BASE_EXPORT WeakReferenceOwner {
|
| void Invalidate();
|
|
|
| private:
|
| + bool bound_to_constructor_sequence_;
|
| mutable scoped_refptr<WeakReference::Flag> flag_;
|
| };
|
|
|
| @@ -259,8 +260,7 @@ class WeakPtr : public internal::WeakPtrBase {
|
| template <class T>
|
| class WeakPtrFactory {
|
| public:
|
| - explicit WeakPtrFactory(T* ptr) : ptr_(ptr) {
|
| - }
|
| + explicit WeakPtrFactory(T* ptr) : weak_reference_owner_(false), ptr_(ptr) {}
|
|
|
| ~WeakPtrFactory() {
|
| ptr_ = NULL;
|
| @@ -289,15 +289,18 @@ class WeakPtrFactory {
|
| DISALLOW_IMPLICIT_CONSTRUCTORS(WeakPtrFactory);
|
| };
|
|
|
| -// A class may extend from SupportsWeakPtr to let others take weak pointers to
|
| -// it. This avoids the class itself implementing boilerplate to dispense weak
|
| -// pointers. However, since SupportsWeakPtr's destructor won't invalidate
|
| -// weak pointers to the class until after the derived class' members have been
|
| -// destroyed, its use can lead to subtle use-after-destroy issues.
|
| +// A class may extend from SupportsWeakPtr to dispense weak pointers to itself.
|
| +// This is a convenience class for avoiding the boilerplate of WeakPtrFactory.
|
| +//
|
| +// Major caveat: Weak pointers issued by this class may only be deferenced on
|
| +// the same thread (or sequence) as its constructor. SupportsWeakPtr's
|
| +// destructor doesn't invalidate its weak pointers until after the derived
|
| +// class' members have been destroyed. Multi-threaded use would lead to subtle
|
| +// use-after-destroy issues. Use WeakPtrFactory for multi-threaded use.
|
| template <class T>
|
| class SupportsWeakPtr : public internal::SupportsWeakPtrBase {
|
| public:
|
| - SupportsWeakPtr() {}
|
| + SupportsWeakPtr() : weak_reference_owner_(true) {}
|
|
|
| WeakPtr<T> AsWeakPtr() {
|
| return WeakPtr<T>(weak_reference_owner_.GetRef(), static_cast<T*>(this));
|
|
|