Index: base/memory/weak_ptr.cc |
diff --git a/base/memory/weak_ptr.cc b/base/memory/weak_ptr.cc |
index d9ce86ad18901a0719255c031fae1a1357ad1f2f..2eaa6ea4cfc6f7ee97eb426a9ee0650a5ccfbdc3 100644 |
--- a/base/memory/weak_ptr.cc |
+++ b/base/memory/weak_ptr.cc |
@@ -7,11 +7,13 @@ |
namespace base { |
namespace internal { |
-WeakReference::Flag::Flag() : is_valid_(true) { |
- // Flags only become bound when checked for validity, or invalidated, |
- // so that we can check that later validity/invalidation operations on |
- // the same Flag take place on the same sequenced thread. |
- sequence_checker_.DetachFromSequence(); |
+WeakReference::Flag::Flag(bool bound_to_current_sequence) : is_valid_(true) { |
+ if (!bound_to_current_sequence) { |
+ // Flags only become bound when checked for validity, or invalidated, |
+ // so that we can check that later validity/invalidation operations on |
+ // the same Flag take place on the same sequenced thread. |
+ sequence_checker_.DetachFromSequence(); |
+ } |
} |
void WeakReference::Flag::Invalidate() { |
@@ -42,7 +44,10 @@ WeakReference::~WeakReference() { |
bool WeakReference::is_valid() const { return flag_.get() && flag_->IsValid(); } |
-WeakReferenceOwner::WeakReferenceOwner() { |
+WeakReferenceOwner::WeakReferenceOwner(bool bound_to_current_sequence) |
+ : bound_to_constructor_sequence_(bound_to_current_sequence) { |
+ if (bound_to_current_sequence) |
+ flag_ = new WeakReference::Flag(bound_to_current_sequence); |
} |
WeakReferenceOwner::~WeakReferenceOwner() { |
@@ -50,9 +55,10 @@ WeakReferenceOwner::~WeakReferenceOwner() { |
} |
WeakReference WeakReferenceOwner::GetRef() const { |
- // If we hold the last reference to the Flag then create a new one. |
- if (!HasRefs()) |
- flag_ = new WeakReference::Flag(); |
+ // If not bound to the constructor sequence, and there are no outstanding weak |
+ // pointers, recreate flag to allow re-binding to a different sequence. |
+ if (!bound_to_constructor_sequence_ && !HasRefs()) |
+ flag_ = new WeakReference::Flag(false /* bound_to_constructor_sequence_ */); |
return WeakReference(flag_.get()); |
} |