Fix LifeCycleWatcher in ScopedVectorTest to not be referenced after being destructed.
If the LifeCycleObject owned by LifeCycleWatcher still exists at the
time LifeCycleWatcher is destroyed, the LifeCycleObject's destructor
will call LifeCycleWatcher::OnLifeCycleDestroy() after
LifeCycleWatcher's destructor has finished running.
BUG=535321
Review URL: https://codereview.chromium.org/1365893002
Cr-Commit-Position: refs/heads/master@{#350683}
CrOS-Libchrome-Original-Commit: 8723a2c2a90e060006d1b45a6ce58bacf17bb9d8
diff --git a/base/memory/scoped_vector_unittest.cc b/base/memory/scoped_vector_unittest.cc
index 220cfb0..4dee9c9 100644
--- a/base/memory/scoped_vector_unittest.cc
+++ b/base/memory/scoped_vector_unittest.cc
@@ -24,7 +24,8 @@
};
~LifeCycleObject() {
- observer_->OnLifeCycleDestroy(this);
+ if (observer_)
+ observer_->OnLifeCycleDestroy(this);
}
private:
@@ -35,6 +36,10 @@
observer_->OnLifeCycleConstruct(this);
}
+ void DisconnectObserver() {
+ observer_ = nullptr;
+ }
+
Observer* observer_;
DISALLOW_COPY_AND_ASSIGN(LifeCycleObject);
@@ -62,7 +67,13 @@
class LifeCycleWatcher : public LifeCycleObject::Observer {
public:
LifeCycleWatcher() : life_cycle_state_(LC_INITIAL) {}
- ~LifeCycleWatcher() override {}
+ ~LifeCycleWatcher() override {
+ // Stop watching the watched object. Without this, the object's destructor
+ // will call into OnLifeCycleDestroy when destructed, which happens after
+ // this destructor has finished running.
+ if (constructed_life_cycle_object_)
+ constructed_life_cycle_object_->DisconnectObserver();
+ }
// Assert INITIAL -> CONSTRUCTED and no LifeCycleObject associated with this
// LifeCycleWatcher.