Index: base/memory/weak_ptr.cc |
diff --git a/base/memory/weak_ptr.cc b/base/memory/weak_ptr.cc |
index 2467abee4078ff64cf219710a95d811e052a0e5b..8879651e6da7bfca1e4ce605c74e62bb69e259af 100644 |
--- a/base/memory/weak_ptr.cc |
+++ b/base/memory/weak_ptr.cc |
@@ -4,55 +4,50 @@ |
#include "base/memory/weak_ptr.h" |
-#include "base/debug/leak_annotations.h" |
- |
namespace base { |
namespace internal { |
-static constexpr uintptr_t kTrueMask = ~static_cast<uintptr_t>(0); |
- |
-WeakReference::Flag::Flag() : is_valid_(kTrueMask) { |
-#if DCHECK_IS_ON() |
+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(); |
-#endif |
} |
-WeakReference::Flag::Flag(WeakReference::Flag::NullFlagTag) : is_valid_(false) { |
- // There is no need for sequence_checker_.DetachFromSequence() because the |
- // null flag doesn't participate in the sequence checks. See DCHECK in |
- // Invalidate() and IsValid(). |
+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(sequence_checker_.CalledOnValidSequence() || HasOneRef()) |
+ << "WeakPtrs must be invalidated on the same sequenced thread."; |
+ is_valid_ = false; |
+} |
- // Keep the object alive perpetually, even when there are no references to it. |
- AddRef(); |
+bool WeakReference::Flag::IsValid() const { |
+ DCHECK(sequence_checker_.CalledOnValidSequence()) |
+ << "WeakPtrs must be checked on the same sequenced thread."; |
+ return is_valid_; |
} |
-WeakReference::Flag* WeakReference::Flag::NullFlag() { |
- ANNOTATE_SCOPED_MEMORY_LEAK; |
- static Flag* g_null_flag = new Flag(kNullFlagTag); |
- return g_null_flag; |
+WeakReference::Flag::~Flag() { |
} |
-WeakReference::Flag::~Flag() {} |
+WeakReference::WeakReference() { |
+} |
-WeakReference::WeakReference() : flag_(Flag::NullFlag()) {} |
+WeakReference::WeakReference(const Flag* flag) : flag_(flag) { |
+} |
WeakReference::~WeakReference() { |
} |
-WeakReference::WeakReference(const Flag* flag) : flag_(flag) {} |
- |
-WeakReference::WeakReference(WeakReference&& other) |
- : flag_(std::move(other.flag_)) { |
- other.flag_ = Flag::NullFlag(); |
-} |
+WeakReference::WeakReference(WeakReference&& other) = default; |
WeakReference::WeakReference(const WeakReference& other) = default; |
-WeakReferenceOwner::WeakReferenceOwner() |
- : flag_(WeakReference::Flag::NullFlag()) {} |
+bool WeakReference::is_valid() const { return flag_.get() && flag_->IsValid(); } |
+ |
+WeakReferenceOwner::WeakReferenceOwner() { |
+} |
WeakReferenceOwner::~WeakReferenceOwner() { |
Invalidate(); |
@@ -67,8 +62,10 @@ WeakReference WeakReferenceOwner::GetRef() const { |
} |
void WeakReferenceOwner::Invalidate() { |
- flag_->Invalidate(); |
- flag_ = WeakReference::Flag::NullFlag(); |
+ if (flag_.get()) { |
+ flag_->Invalidate(); |
+ flag_ = NULL; |
+ } |
} |
WeakPtrBase::WeakPtrBase() : ptr_(0) {} |