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

Unified Diff: base/observer_list.h

Issue 7127001: Fix user-after-free error with ObserverList. The problem is that if an ObserverListBase::Iterato... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Created 9 years, 6 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | base/observer_list_unittest.cc » ('j') | content/browser/renderer_host/render_view_host.cc » ('J')
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/observer_list.h
===================================================================
--- base/observer_list.h (revision 87996)
+++ base/observer_list.h (working copy)
@@ -12,6 +12,7 @@
#include "base/basictypes.h"
#include "base/logging.h"
+#include "base/memory/weak_ptr.h"
///////////////////////////////////////////////////////////////////////////////
//
@@ -61,7 +62,8 @@
class ObserverListThreadSafe;
template <class ObserverType>
-class ObserverListBase {
+class ObserverListBase
+ : public base::SupportsWeakPtr<ObserverListBase<ObserverType> > {
public:
// Enumeration of which observers are notified.
enum NotificationType {
@@ -79,21 +81,23 @@
class Iterator {
public:
Iterator(ObserverListBase<ObserverType>& list)
- : list_(list),
+ : list_(list.AsWeakPtr()),
index_(0),
max_index_(list.type_ == NOTIFY_ALL ?
std::numeric_limits<size_t>::max() :
list.observers_.size()) {
- ++list_.notify_depth_;
+ ++list_->notify_depth_;
}
~Iterator() {
- if (--list_.notify_depth_ == 0)
- list_.Compact();
+ if (list_ && --list_->notify_depth_ == 0)
+ list_->Compact();
}
ObserverType* GetNext() {
- ListType& observers = list_.observers_;
+ if (!list_)
+ return NULL;
+ ListType& observers = list_->observers_;
// Advance if the current element is null
size_t max_index = std::min(max_index_, observers.size());
while (index_ < max_index && !observers[index_])
@@ -102,7 +106,7 @@
}
private:
- ObserverListBase<ObserverType>& list_;
+ base::WeakPtr<ObserverListBase<ObserverType> > list_;
size_t index_;
size_t max_index_;
};
« no previous file with comments | « no previous file | base/observer_list_unittest.cc » ('j') | content/browser/renderer_host/render_view_host.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698