Index: base/memory/weak_ptr.cc |
diff --git a/base/memory/weak_ptr.cc b/base/memory/weak_ptr.cc |
index 9dec8fd6db9c29956673a79326efa4b22af2f3ad..321b0576b2555ed995ac8a39c74bd40d944f155f 100644 |
--- a/base/memory/weak_ptr.cc |
+++ b/base/memory/weak_ptr.cc |
@@ -8,17 +8,23 @@ 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 thread. |
+ thread_checker_.DetachFromThread(); |
} |
void WeakReference::Flag::Invalidate() { |
// The flag being invalidated with a single ref implies that there are no |
// weak pointers in existence. Allow deletion on other thread in this case. |
- DCHECK(thread_checker_.CalledOnValidThread() || HasOneRef()); |
+ DCHECK(thread_checker_.CalledOnValidThread() || HasOneRef()) |
+ << "WeakPtrs must be checked and invalidated on the same thread."; |
is_valid_ = false; |
} |
bool WeakReference::Flag::IsValid() const { |
- DCHECK(thread_checker_.CalledOnValidThread()); |
+ DCHECK(thread_checker_.CalledOnValidThread()) |
+ << "WeakPtrs must be checked and invalidated on the same thread."; |
return is_valid_; |
} |
@@ -46,10 +52,10 @@ WeakReferenceOwner::~WeakReferenceOwner() { |
} |
WeakReference WeakReferenceOwner::GetRef() const { |
- // We also want to reattach to the current thread if all previous references |
- // have gone away. |
+ // If we hold the last reference to the Flag then create a new one. |
if (!HasRefs()) |
flag_ = new WeakReference::Flag(); |
+ |
return WeakReference(flag_); |
} |