diff options
author | willchan@chromium.org <willchan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-07-20 16:01:32 +0000 |
---|---|---|
committer | willchan@chromium.org <willchan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-07-20 16:01:32 +0000 |
commit | 8d5f3ace1907853bd6e838d4f061c23a526b5384 (patch) | |
tree | b7c3957aa213f0a9fcbfd9b3a65fb0ae852ed76b /base/message_pump_libevent.cc | |
parent | 9abfa1903f72e0735c9c1ef6570eef94f26eb4a7 (diff) | |
download | chromium_src-8d5f3ace1907853bd6e838d4f061c23a526b5384.zip chromium_src-8d5f3ace1907853bd6e838d4f061c23a526b5384.tar.gz chromium_src-8d5f3ace1907853bd6e838d4f061c23a526b5384.tar.bz2 |
Handle object deletion in FileDescriptorWatcher callback.
BUG=88134
TEST=MessagePumpLibeventTest.*
Review URL: http://codereview.chromium.org/7398036
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@93201 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/message_pump_libevent.cc')
-rw-r--r-- | base/message_pump_libevent.cc | 16 |
1 files changed, 12 insertions, 4 deletions
diff --git a/base/message_pump_libevent.cc b/base/message_pump_libevent.cc index c48f490..3e3facf 100644 --- a/base/message_pump_libevent.cc +++ b/base/message_pump_libevent.cc @@ -8,6 +8,7 @@ #include <fcntl.h> #include "base/auto_reset.h" +#include "base/compiler_specific.h" #include "base/eintr_wrapper.h" #include "base/logging.h" #include "base/mac/scoped_nsautorelease_pool.h" @@ -53,7 +54,7 @@ MessagePumpLibevent::FileDescriptorWatcher::FileDescriptorWatcher() : is_persistent_(false), event_(NULL), pump_(NULL), - watcher_(NULL) { + ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { } MessagePumpLibevent::FileDescriptorWatcher::~FileDescriptorWatcher() { @@ -92,6 +93,10 @@ event *MessagePumpLibevent::FileDescriptorWatcher::ReleaseEvent() { void MessagePumpLibevent::FileDescriptorWatcher::OnFileCanReadWithoutBlocking( int fd, MessagePumpLibevent* pump) { + // Since OnFileCanWriteWithoutBlocking() gets called first, it can stop + // watching the file descriptor. + if (!watcher_) + return; pump->WillProcessIOEvent(); watcher_->OnFileCanReadWithoutBlocking(fd); pump->DidProcessIOEvent(); @@ -99,6 +104,7 @@ void MessagePumpLibevent::FileDescriptorWatcher::OnFileCanReadWithoutBlocking( void MessagePumpLibevent::FileDescriptorWatcher::OnFileCanWriteWithoutBlocking( int fd, MessagePumpLibevent* pump) { + DCHECK(watcher_); pump->WillProcessIOEvent(); watcher_->OnFileCanWriteWithoutBlocking(fd); pump->DidProcessIOEvent(); @@ -334,8 +340,9 @@ bool MessagePumpLibevent::Init() { // static void MessagePumpLibevent::OnLibeventNotification(int fd, short flags, void* context) { - FileDescriptorWatcher* controller = - static_cast<FileDescriptorWatcher*>(context); + base::WeakPtr<FileDescriptorWatcher> controller = + static_cast<FileDescriptorWatcher*>(context)->weak_factory_.GetWeakPtr(); + DCHECK(controller.get()); MessagePumpLibevent* pump = controller->pump(); pump->processed_io_events_ = true; @@ -343,7 +350,8 @@ void MessagePumpLibevent::OnLibeventNotification(int fd, short flags, if (flags & EV_WRITE) { controller->OnFileCanWriteWithoutBlocking(fd, pump); } - if (flags & EV_READ) { + // Check |controller| in case it's been deleted in this callback. + if (controller.get() && flags & EV_READ) { controller->OnFileCanReadWithoutBlocking(fd, pump); } } |