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

Side by Side Diff: tools/android/forwarder2/self_deleter_helper.h

Issue 555093002: Fix WeakPtrFactory ordering problems in src/tools (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Added the comment explaining WeakPtrFactory ordering Created 6 years, 3 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 unified diff | Download patch
« no previous file with comments | « tools/android/forwarder2/device_controller.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef TOOLS_ANDROID_FORWARDER2_SELF_DELETER_HELPER_H_ 5 #ifndef TOOLS_ANDROID_FORWARDER2_SELF_DELETER_HELPER_H_
6 #define TOOLS_ANDROID_FORWARDER2_SELF_DELETER_HELPER_H_ 6 #define TOOLS_ANDROID_FORWARDER2_SELF_DELETER_HELPER_H_
7 7
8 #include "base/basictypes.h" 8 #include "base/basictypes.h"
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/callback.h" 10 #include "base/callback.h"
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
92 // scoped_ptr<Object> object_; 92 // scoped_ptr<Object> object_;
93 // }; 93 // };
94 // 94 //
95 template <typename T> 95 template <typename T>
96 class SelfDeleterHelper { 96 class SelfDeleterHelper {
97 public: 97 public:
98 typedef base::Callback<void (scoped_ptr<T>)> DeletionCallback; 98 typedef base::Callback<void (scoped_ptr<T>)> DeletionCallback;
99 99
100 SelfDeleterHelper(T* self_deleting_object, 100 SelfDeleterHelper(T* self_deleting_object,
101 const DeletionCallback& deletion_callback) 101 const DeletionCallback& deletion_callback)
102 : weak_ptr_factory_(this), 102 : construction_runner_(base::MessageLoopProxy::current()),
103 construction_runner_(base::MessageLoopProxy::current()),
104 self_deleting_object_(self_deleting_object), 103 self_deleting_object_(self_deleting_object),
105 deletion_callback_(deletion_callback) { 104 deletion_callback_(deletion_callback),
105 weak_ptr_factory_(this) {
106 } 106 }
107 107
108 ~SelfDeleterHelper() { 108 ~SelfDeleterHelper() {
109 DCHECK(construction_runner_->RunsTasksOnCurrentThread()); 109 DCHECK(construction_runner_->RunsTasksOnCurrentThread());
110 } 110 }
111 111
112 void MaybeSelfDeleteSoon() { 112 void MaybeSelfDeleteSoon() {
113 DCHECK(!construction_runner_->RunsTasksOnCurrentThread()); 113 DCHECK(!construction_runner_->RunsTasksOnCurrentThread());
114 construction_runner_->PostTask( 114 construction_runner_->PostTask(
115 FROM_HERE, 115 FROM_HERE,
116 base::Bind(&SelfDeleterHelper::SelfDelete, 116 base::Bind(&SelfDeleterHelper::SelfDelete,
117 weak_ptr_factory_.GetWeakPtr())); 117 weak_ptr_factory_.GetWeakPtr()));
118 } 118 }
119 119
120 private: 120 private:
121 void SelfDelete() { 121 void SelfDelete() {
122 DCHECK(construction_runner_->RunsTasksOnCurrentThread()); 122 DCHECK(construction_runner_->RunsTasksOnCurrentThread());
123 deletion_callback_.Run(make_scoped_ptr(self_deleting_object_)); 123 deletion_callback_.Run(make_scoped_ptr(self_deleting_object_));
124 } 124 }
125 125
126 base::WeakPtrFactory<SelfDeleterHelper<T> > weak_ptr_factory_;
127 const scoped_refptr<base::SingleThreadTaskRunner> construction_runner_; 126 const scoped_refptr<base::SingleThreadTaskRunner> construction_runner_;
128 T* const self_deleting_object_; 127 T* const self_deleting_object_;
129 const DeletionCallback deletion_callback_; 128 const DeletionCallback deletion_callback_;
130 129
130 //WeakPtrFactory's documentation says:
131 // Member variables should appear before the WeakPtrFactory, to ensure
132 // that any WeakPtrs to Controller are invalidated before its members
133 // variable's destructors are executed, rendering them invalid.
134 base::WeakPtrFactory<SelfDeleterHelper<T> > weak_ptr_factory_;
135
131 DISALLOW_COPY_AND_ASSIGN(SelfDeleterHelper); 136 DISALLOW_COPY_AND_ASSIGN(SelfDeleterHelper);
132 }; 137 };
133 138
134 } // namespace forwarder2 139 } // namespace forwarder2
135 140
136 #endif // TOOLS_ANDROID_FORWARDER2_SELF_DELETER_HELPER_H_ 141 #endif // TOOLS_ANDROID_FORWARDER2_SELF_DELETER_HELPER_H_
OLDNEW
« no previous file with comments | « tools/android/forwarder2/device_controller.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698