Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(140)

Unified Diff: base/memory/weak_ptr.cc

Issue 1602443003: Improve GetWeakPtr() to better protect against unsafe usage patterns. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « base/memory/weak_ptr.h ('k') | base/memory/weak_ptr_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/memory/weak_ptr.cc
diff --git a/base/memory/weak_ptr.cc b/base/memory/weak_ptr.cc
index c179b80097cc674e1ad2e484b99909166dda860a..6ec4fd3cf359ae0ad3c20a806b5496d389bb4642 100644
--- a/base/memory/weak_ptr.cc
+++ b/base/memory/weak_ptr.cc
@@ -15,9 +15,7 @@ WeakReference::Flag::Flag() : is_valid_(true) {
}
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())
+ DCHECK(sequence_checker_.CalledOnValidSequence())
<< "WeakPtrs must be invalidated on the same sequenced thread.";
is_valid_ = false;
}
@@ -28,14 +26,18 @@ bool WeakReference::Flag::IsValid() const {
return is_valid_;
}
+void WeakReference::Flag::DetachFromSequence() {
+ DCHECK(HasOneRef()) << "Cannot detach from Sequence while WeakPtrs exist.";
+ sequence_checker_.DetachFromSequence();
+}
+
WeakReference::Flag::~Flag() {
}
WeakReference::WeakReference() {
}
-WeakReference::WeakReference(const Flag* flag) : flag_(flag) {
-}
+WeakReference::WeakReference(const scoped_refptr<Flag>& flag) : flag_(flag) {}
WeakReference::~WeakReference() {
}
@@ -44,7 +46,9 @@ WeakReference::WeakReference(WeakReference&& other) = default;
WeakReference::WeakReference(const WeakReference& other) = default;
-bool WeakReference::is_valid() const { return flag_.get() && flag_->IsValid(); }
+bool WeakReference::is_valid() const {
+ return flag_ && flag_->IsValid();
+}
WeakReferenceOwner::WeakReferenceOwner() {
}
@@ -54,20 +58,23 @@ WeakReferenceOwner::~WeakReferenceOwner() {
}
WeakReference WeakReferenceOwner::GetRef() const {
- // If we hold the last reference to the Flag then create a new one.
- if (!HasRefs())
+ if (!flag_)
flag_ = new WeakReference::Flag();
- return WeakReference(flag_.get());
+ return WeakReference(flag_);
}
void WeakReferenceOwner::Invalidate() {
- if (flag_.get()) {
+ if (flag_) {
flag_->Invalidate();
- flag_ = NULL;
+ flag_ = nullptr;
}
}
+void WeakReferenceOwner::DetachFromSequence() {
+ flag_->DetachFromSequence();
+}
+
WeakPtrBase::WeakPtrBase() {
}
« no previous file with comments | « base/memory/weak_ptr.h ('k') | base/memory/weak_ptr_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698