Skip to content

Commit

Permalink
Update WeakPtrFactory examples to use in-class initializers.
Browse files Browse the repository at this point in the history
This is now the more common, and simpler, pattern.

Bug: 981415
Change-Id: I082e75425a33f6256c2ddc3228d61f8b6fe8bc58
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1704677
Reviewed-by: Gabriel Charette <gab@chromium.org>
Commit-Queue: Jeremy Roman <jbroman@chromium.org>
Cr-Commit-Position: refs/heads/master@{#677960}
  • Loading branch information
jeremyroman authored and Commit Bot committed Jul 16, 2019
1 parent 121548c commit 0dd0b2f
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 7 deletions.
3 changes: 1 addition & 2 deletions base/memory/weak_ptr.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,13 @@
//
// class Controller {
// public:
// Controller() : weak_factory_(this) {}
// void SpawnWorker() { Worker::StartNew(weak_factory_.GetWeakPtr()); }
// void WorkComplete(const Result& result) { ... }
// private:
// // Member variables should appear before the WeakPtrFactory, to ensure
// // that any WeakPtrs to Controller are invalidated before its members
// // variable's destructors are executed, rendering them invalid.
// WeakPtrFactory<Controller> weak_factory_;
// WeakPtrFactory<Controller> weak_factory_{this};
// };
//
// class Worker {
Expand Down
4 changes: 2 additions & 2 deletions docs/callback.md
Original file line number Diff line number Diff line change
Expand Up @@ -355,13 +355,13 @@ To use `base::WeakPtr` with `base::Bind()`, `MyClass` will typically look like:
```cpp
class MyClass {
public:
MyClass() : weak_factory_(this) {
MyClass() {
weak_this_ = weak_factory_.GetWeakPtr();
}
private:
base::WeakPtr<MyClass> weak_this_;
// MyClass member variables go here.
base::WeakPtrFactory<MyClass> weak_factory_;
base::WeakPtrFactory<MyClass> weak_factory_{this};
};
```

Expand Down
4 changes: 1 addition & 3 deletions docs/threading_and_tasks.md
Original file line number Diff line number Diff line change
Expand Up @@ -588,8 +588,6 @@ int Compute() { … }
class A {
public:
A() : weak_ptr_factory_(this) {}
void ComputeAndStore() {
// Schedule a call to Compute() in a thread pool followed by
// a call to A::Store() on the current sequence. The call to
Expand All @@ -604,7 +602,7 @@ class A {
void Store(int value) { value_ = value; }
int value_;
base::WeakPtrFactory<A> weak_ptr_factory_;
base::WeakPtrFactory<A> weak_ptr_factory_{this};
};
```

Expand Down

0 comments on commit 0dd0b2f

Please sign in to comment.