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

Issue 183026: Add a WeakPtr<T> class.... (Closed)

Created:
11 years, 3 months ago by darin (slow to review)
Modified:
9 years, 7 months ago
Reviewers:
brettw
CC:
chromium-reviews_googlegroups.com, brettw, Paweł Hajdan Jr.
Visibility:
Public.

Description

Add a WeakPtr<T> class. This allows a class to hand out weak pointers to itself that will be nulled out automatically when the class instance is destroyed. I have provided two ways for a class to implement weak pointers. It can either subclass SupportsWeakPtr (like subclassing RefCounted) or it can be composed of WeakPtrFactory (like having a ScopedRunnableMethodFactory member). Eventually, I'd like to make it possible to pass a WeakPtr<T> as the first parameter to NewRunnableMethod. This will make ScopedRunnableMethodFactory obsolete and should help cleanup some code. One feature that makes the implementation here a bit more complicated is that it is possible to pass a WeakPtr<U> to a method that takes a WeakPtr<T> provided U "is a" T. This proved useful in RenderView, which can then give out weak references to both itself as well as to an interface it implements. This informed the design of WeakPtr, causing it to have a T* ptr_ member instead of stashing that pointer within the ref counted WeakReference object. R=brettw BUG=none TEST=weak_ptr_unittest.cc Committed: http://src.chromium.org/viewvc/chrome?view=rev&revision=25087

Patch Set 1 #

Patch Set 2 : '' #

Patch Set 3 : '' #

Patch Set 4 : '' #

Total comments: 14

Patch Set 5 : '' #

Total comments: 3

Patch Set 6 : '' #

Patch Set 7 : '' #

Unified diffs Side-by-side diffs Delta from patch set Stats (+345 lines, -0 lines) Patch
M base/base.gyp View 1 2 2 chunks +2 lines, -0 lines 0 comments Download
A base/weak_ptr.h View 1 2 3 4 5 1 chunk +221 lines, -0 lines 0 comments Download
A base/weak_ptr_unittest.cc View 1 2 3 4 5 6 1 chunk +122 lines, -0 lines 0 comments Download

Messages

Total messages: 6 (0 generated)
darin (slow to review)
11 years, 3 months ago (2009-09-01 16:44:50 UTC) #1
brettw
http://codereview.chromium.org/183026/diff/7001/7003 File base/weak_ptr.h (right): http://codereview.chromium.org/183026/diff/7001/7003#newcode48 Line 48: // WARNING: weak pointers are not threadsafe!!! Can ...
11 years, 3 months ago (2009-09-01 17:23:49 UTC) #2
darin (slow to review)
http://codereview.chromium.org/183026/diff/7001/7003 File base/weak_ptr.h (right): http://codereview.chromium.org/183026/diff/7001/7003#newcode48 Line 48: // WARNING: weak pointers are not threadsafe!!! On ...
11 years, 3 months ago (2009-09-01 17:38:34 UTC) #3
brettw
http://codereview.chromium.org/183026/diff/4006/4008 File base/weak_ptr.h (right): http://codereview.chromium.org/183026/diff/4006/4008#newcode191 Line 191: void InvalidateWeakPtrs() { weak_reference_owner_.Invalidate(); } I agree that ...
11 years, 3 months ago (2009-09-01 17:49:58 UTC) #4
darin (slow to review)
http://codereview.chromium.org/183026/diff/4006/4008 File base/weak_ptr.h (right): http://codereview.chromium.org/183026/diff/4006/4008#newcode191 Line 191: void InvalidateWeakPtrs() { weak_reference_owner_.Invalidate(); } On 2009/09/01 17:49:58, ...
11 years, 3 months ago (2009-09-01 18:13:12 UTC) #5
brettw
11 years, 3 months ago (2009-09-01 18:32:37 UTC) #6
http://codereview.chromium.org/183026/diff/4006/4008
File base/weak_ptr.h (right):

http://codereview.chromium.org/183026/diff/4006/4008#newcode191
Line 191: void InvalidateWeakPtrs() { weak_reference_owner_.Invalidate(); }
On 2009/09/01 18:13:12, darin wrote:
> On 2009/09/01 17:49:58, brettw wrote:
> > I agree that there are a bunch of callers, but those are all for the scoped
> > factory, and the member for the factory is always inside some other class.
In
> > this case, you're exposing RevokeAll to any users of the class, which I
don't
> > think we have now, and I don't think we want.
> 
> So, you support the method for WeakPtrFactory but not for SupportsWeakPtr? 
I'm
> okay with that.

Yes. LGTM with this.

Powered by Google App Engine
This is Rietveld 408576698