Skip to content

Commit

Permalink
[docs] Tidy up docs/callback.md wording around use of WeakPtr.
Browse files Browse the repository at this point in the history
The documentation for Bind() made misleading statements about WeakPtr
thread-safety, leading to confusion over correct usage.

Change-Id: I8d4226e789150e56e994d3491db0b1ef3d2881dd
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1652552
Reviewed-by: Albert J. Wong <ajwong@chromium.org>
Commit-Queue: Wez <wez@chromium.org>
Auto-Submit: Wez <wez@chromium.org>
Cr-Commit-Position: refs/heads/master@{#671139}
  • Loading branch information
Wez authored and Commit Bot committed Jun 21, 2019
1 parent 9210f60 commit 3327626
Showing 1 changed file with 31 additions and 6 deletions.
37 changes: 31 additions & 6 deletions docs/callback.md
Original file line number Diff line number Diff line change
Expand Up @@ -337,17 +337,42 @@ base::BindRepeating(&Foo, base::Passed(std::move(p))); // Ok, but subtle.

### Binding A Class Method With Weak Pointers

If `MyClass` has a `base::WeakPtr<MyClass> weak_this_` member (see below)
then a class method can be bound with:

```cpp
base::Bind(&MyClass::Foo, GetWeakPtr());
base::Bind(&MyClass::Foo, weak_this_);
```
The callback will not be run if the object has already been destroyed.
**DANGER**: weak pointers are not threadsafe, so don't use this when passing
between threads!
To make a weak pointer, you would typically create a
`base::WeakPtrFactory<Foo>` member at the bottom (to ensure it's destroyed
last) of class `Foo`, then call `weak_factory_.GetWeakPtr()`.
Note that class method callbacks bound to `base::WeakPtr`s may only be
run on the same sequence on which the object will be destroyed, since otherwise
execution of the callback might race with the object's deletion.
To use `base::WeakPtr` with `base::Bind()`, `MyClass` will typically look like:
```cpp
class MyClass {
public:
MyClass() : weak_factory_(this) {
weak_this_ = weak_factory_.GetWeakPtr();
}
private:
base::WeakPtr<MyClass> weak_this_;
// MyClass member variables go here.
base::WeakPtrFactory<MyClass> weak_factory_;
};
```

`weak_factory_` is the last member variable in `MyClass` so that it is
destroyed first. This ensures that if any class methods bound to `weak_this_`
are `Run()` during teardown, then they will not actually be executed.

If `MyClass` only ever `base::Bind()`s and executes callbacks on the same
sequence, then it is generally safe to call `weak_factory_.GetWeakPtr()` at the
`base::Bind()` call, rather than taking a separate `weak_this_` during
construction.

### Binding A Class Method With Manual Lifetime Management

Expand Down

0 comments on commit 3327626

Please sign in to comment.