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

Unified Diff: base/memory/weak_ptr.h

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 | « no previous file | base/memory/weak_ptr.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/memory/weak_ptr.h
diff --git a/base/memory/weak_ptr.h b/base/memory/weak_ptr.h
index 3544439dd3cafc9d8cc849a2cb4181cf70d2e947..aa5e5a17b1a07505d40ba44d2c07c56693e134fe 100644
--- a/base/memory/weak_ptr.h
+++ b/base/memory/weak_ptr.h
@@ -99,6 +99,8 @@ class BASE_EXPORT WeakReference {
void Invalidate();
bool IsValid() const;
+ void DetachFromSequence();
+
private:
friend class base::RefCountedThreadSafe<Flag>;
@@ -109,7 +111,7 @@ class BASE_EXPORT WeakReference {
};
WeakReference();
- explicit WeakReference(const Flag* flag);
+ explicit WeakReference(const scoped_refptr<Flag>& flag);
~WeakReference();
WeakReference(WeakReference&& other);
@@ -130,12 +132,12 @@ class BASE_EXPORT WeakReferenceOwner {
WeakReference GetRef() const;
- bool HasRefs() const {
- return flag_.get() && !flag_->HasOneRef();
- }
+ bool HasRefs() const { return flag_ && !flag_->HasOneRef(); }
void Invalidate();
+ void DetachFromSequence();
+
private:
mutable scoped_refptr<WeakReference::Flag> flag_;
};
@@ -288,18 +290,24 @@ class WeakPtrFactory {
~WeakPtrFactory() { ptr_ = nullptr; }
+ // Returns a new WeakPtr. Although GetWeakPtr() can be called from any thread
+ // (assuming calling code ensures the WeakPtrFactory is still live at the
+ // time), it is not safe to call concurrently from separate threads. Code
+ // which hands-off WeakPtrs to separate threads for them to Bind() into
+ // posted tasks, for example, should usually take a WeakPtr during setup
+ // and store it for later use, rather than call GetWeakPtr() at Bind() time.
WeakPtr<T> GetWeakPtr() {
DCHECK(ptr_);
return WeakPtr<T>(weak_reference_owner_.GetRef(), ptr_);
}
- // Call this method to invalidate all existing weak pointers.
+ // Invalidates all existing weak pointers issued by this factory.
void InvalidateWeakPtrs() {
DCHECK(ptr_);
weak_reference_owner_.Invalidate();
}
- // Call this method to determine if any weak pointers exist.
+ // Returns true if one or more WeakPtrs issued by this factory still exist.
bool HasWeakPtrs() const {
DCHECK(ptr_);
return weak_reference_owner_.HasRefs();
@@ -328,6 +336,11 @@ class SupportsWeakPtr : public internal::SupportsWeakPtrBase {
protected:
~SupportsWeakPtr() {}
+ // Asserts that no issued WeakPtrs exist, and detaches the sequence binding.
+ void DetachFromSequence() { weak_reference_owner_.DetachFromSequence(); }
+
+ bool HasWeakPtrs() const { return weak_reference_owner_.HasRefs(); }
+
private:
internal::WeakReferenceOwner weak_reference_owner_;
DISALLOW_COPY_AND_ASSIGN(SupportsWeakPtr);
« no previous file with comments | « no previous file | base/memory/weak_ptr.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698