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

Unified Diff: base/memory/weak_ptr.h

Issue 692233003: Base: Bind SupportsWeakPtr's references to the constructor's thread. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 1 month 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 8a433922fdf0009f1bc4ed0a1a0caa146a798977..dec26b931fa6a760bbf0cbd20af86e09f87ad3bb 100644
--- a/base/memory/weak_ptr.h
+++ b/base/memory/weak_ptr.h
@@ -86,7 +86,7 @@ class BASE_EXPORT WeakReference {
// deleted from another via base::WeakPtr::~WeakPtr().
class BASE_EXPORT Flag : public RefCountedThreadSafe<Flag> {
public:
- Flag();
+ Flag(bool bound_to_constructor_sequence);
void Invalidate();
bool IsValid() const;
@@ -112,7 +112,7 @@ class BASE_EXPORT WeakReference {
class BASE_EXPORT WeakReferenceOwner {
public:
- WeakReferenceOwner();
+ WeakReferenceOwner(bool bound_to_constructor_sequence);
~WeakReferenceOwner();
WeakReference GetRef() const;
@@ -124,6 +124,7 @@ class BASE_EXPORT WeakReferenceOwner {
void Invalidate();
private:
+ bool bound_to_constructor_sequence_;
mutable scoped_refptr<WeakReference::Flag> flag_;
};
@@ -259,8 +260,7 @@ class WeakPtr : public internal::WeakPtrBase {
template <class T>
class WeakPtrFactory {
public:
- explicit WeakPtrFactory(T* ptr) : ptr_(ptr) {
- }
+ explicit WeakPtrFactory(T* ptr) : weak_reference_owner_(false), ptr_(ptr) {}
~WeakPtrFactory() {
ptr_ = NULL;
@@ -289,15 +289,18 @@ class WeakPtrFactory {
DISALLOW_IMPLICIT_CONSTRUCTORS(WeakPtrFactory);
};
-// A class may extend from SupportsWeakPtr to let others take weak pointers to
-// it. This avoids the class itself implementing boilerplate to dispense weak
-// pointers. However, since SupportsWeakPtr's destructor won't invalidate
-// weak pointers to the class until after the derived class' members have been
-// destroyed, its use can lead to subtle use-after-destroy issues.
+// A class may extend from SupportsWeakPtr to dispense weak pointers to itself.
+// This is a convenience class for avoiding the boilerplate of WeakPtrFactory.
+//
+// Major caveat: Weak pointers issued by this class may only be deferenced on
+// the same thread (or sequence) as its constructor. SupportsWeakPtr's
+// destructor doesn't invalidate its weak pointers until after the derived
+// class' members have been destroyed. Multi-threaded use would lead to subtle
+// use-after-destroy issues. Use WeakPtrFactory for multi-threaded use.
template <class T>
class SupportsWeakPtr : public internal::SupportsWeakPtrBase {
public:
- SupportsWeakPtr() {}
+ SupportsWeakPtr() : weak_reference_owner_(true) {}
WeakPtr<T> AsWeakPtr() {
return WeakPtr<T>(weak_reference_owner_.GetRef(), static_cast<T*>(this));
« 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