Commit efd2db86 authored by anujk.sharma's avatar anujk.sharma Committed by Commit bot

Fix WeakPtrFactory ordering problems in src/tools

Cleaning up weak_ptr_factory destruction order in "src/tools" module.
WeakPtrFactory should remain the last member so it'll be destroyed and
invalidate its weak pointers before any other members are destroyed.

BUG=303818

Review URL: https://codereview.chromium.org/555093002

Cr-Commit-Position: refs/heads/master@{#294117}
parent a8e2fc2b
...@@ -47,10 +47,10 @@ void DeviceController::Start() { ...@@ -47,10 +47,10 @@ void DeviceController::Start() {
DeviceController::DeviceController(scoped_ptr<Socket> host_socket, DeviceController::DeviceController(scoped_ptr<Socket> host_socket,
int exit_notifier_fd) int exit_notifier_fd)
: weak_ptr_factory_(this), : host_socket_(host_socket.Pass()),
host_socket_(host_socket.Pass()),
exit_notifier_fd_(exit_notifier_fd), exit_notifier_fd_(exit_notifier_fd),
construction_task_runner_(base::MessageLoopProxy::current()) { construction_task_runner_(base::MessageLoopProxy::current()),
weak_ptr_factory_(this) {
host_socket_->AddEventFd(exit_notifier_fd); host_socket_->AddEventFd(exit_notifier_fd);
} }
......
...@@ -49,7 +49,6 @@ class DeviceController { ...@@ -49,7 +49,6 @@ class DeviceController {
const base::WeakPtr<DeviceController>& device_controller_ptr, const base::WeakPtr<DeviceController>& device_controller_ptr,
scoped_ptr<DeviceListener> device_listener); scoped_ptr<DeviceListener> device_listener);
base::WeakPtrFactory<DeviceController> weak_ptr_factory_;
const scoped_ptr<Socket> host_socket_; const scoped_ptr<Socket> host_socket_;
// Used to notify the controller to exit. // Used to notify the controller to exit.
const int exit_notifier_fd_; const int exit_notifier_fd_;
...@@ -58,6 +57,12 @@ class DeviceController { ...@@ -58,6 +57,12 @@ class DeviceController {
const scoped_refptr<base::SingleThreadTaskRunner> construction_task_runner_; const scoped_refptr<base::SingleThreadTaskRunner> construction_task_runner_;
ListenersMap listeners_; ListenersMap listeners_;
//WeakPtrFactory's documentation says:
// 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.
base::WeakPtrFactory<DeviceController> weak_ptr_factory_;
DISALLOW_COPY_AND_ASSIGN(DeviceController); DISALLOW_COPY_AND_ASSIGN(DeviceController);
}; };
......
...@@ -99,10 +99,10 @@ class SelfDeleterHelper { ...@@ -99,10 +99,10 @@ class SelfDeleterHelper {
SelfDeleterHelper(T* self_deleting_object, SelfDeleterHelper(T* self_deleting_object,
const DeletionCallback& deletion_callback) const DeletionCallback& deletion_callback)
: weak_ptr_factory_(this), : construction_runner_(base::MessageLoopProxy::current()),
construction_runner_(base::MessageLoopProxy::current()),
self_deleting_object_(self_deleting_object), self_deleting_object_(self_deleting_object),
deletion_callback_(deletion_callback) { deletion_callback_(deletion_callback),
weak_ptr_factory_(this) {
} }
~SelfDeleterHelper() { ~SelfDeleterHelper() {
...@@ -123,11 +123,16 @@ class SelfDeleterHelper { ...@@ -123,11 +123,16 @@ class SelfDeleterHelper {
deletion_callback_.Run(make_scoped_ptr(self_deleting_object_)); deletion_callback_.Run(make_scoped_ptr(self_deleting_object_));
} }
base::WeakPtrFactory<SelfDeleterHelper<T> > weak_ptr_factory_;
const scoped_refptr<base::SingleThreadTaskRunner> construction_runner_; const scoped_refptr<base::SingleThreadTaskRunner> construction_runner_;
T* const self_deleting_object_; T* const self_deleting_object_;
const DeletionCallback deletion_callback_; const DeletionCallback deletion_callback_;
//WeakPtrFactory's documentation says:
// 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.
base::WeakPtrFactory<SelfDeleterHelper<T> > weak_ptr_factory_;
DISALLOW_COPY_AND_ASSIGN(SelfDeleterHelper); DISALLOW_COPY_AND_ASSIGN(SelfDeleterHelper);
}; };
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment