Commit cbb5e9ba authored by Sebastien Marchand's avatar Sebastien Marchand Committed by Commit Bot

SystemMonitor: Unregister the observers when they are destroyed

This will ensure that no observers are being leaked.

Change-Id: I41d20eb4105afb894423113029563a548ec8507e
Reviewed-on: https://chromium-review.googlesource.com/c/1487361Reviewed-by: default avatarFrançois Doray <fdoray@chromium.org>
Commit-Queue: Sébastien Marchand <sebmarchand@chromium.org>
Cr-Commit-Position: refs/heads/master@{#636823}
parent 356a45fa
...@@ -75,6 +75,13 @@ SystemMonitor* SystemMonitor::Get() { ...@@ -75,6 +75,13 @@ SystemMonitor* SystemMonitor::Get() {
return g_system_metrics_monitor; return g_system_metrics_monitor;
} }
SystemMonitor::SystemObserver::~SystemObserver() {
if (g_system_metrics_monitor) {
// This is a no-op if the observer has already been removed.
g_system_metrics_monitor->RemoveObserver(this);
}
}
void SystemMonitor::SystemObserver::OnFreePhysicalMemoryMbSample( void SystemMonitor::SystemObserver::OnFreePhysicalMemoryMbSample(
int free_phys_memory_mb) { int free_phys_memory_mb) {
NOTREACHED(); NOTREACHED();
...@@ -103,7 +110,7 @@ void SystemMonitor::AddOrUpdateObserver( ...@@ -103,7 +110,7 @@ void SystemMonitor::AddOrUpdateObserver(
void SystemMonitor::RemoveObserver(SystemMonitor::SystemObserver* observer) { void SystemMonitor::RemoveObserver(SystemMonitor::SystemObserver* observer) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
observers_.RemoveObserver(observer); observers_.RemoveObserver(observer);
observer_metrics_.erase(observer); if (observer_metrics_.erase(observer))
UpdateObservedMetrics(); UpdateObservedMetrics();
} }
......
...@@ -77,7 +77,7 @@ class SystemMonitor { ...@@ -77,7 +77,7 @@ class SystemMonitor {
SamplingFrequency::kNoSampling; SamplingFrequency::kNoSampling;
}; };
~SystemObserver() override = default; ~SystemObserver() override;
// Reports the amount of free physical memory, in MB. // Reports the amount of free physical memory, in MB.
virtual void OnFreePhysicalMemoryMbSample(int free_phys_memory_mb); virtual void OnFreePhysicalMemoryMbSample(int free_phys_memory_mb);
...@@ -102,7 +102,8 @@ class SystemMonitor { ...@@ -102,7 +102,8 @@ class SystemMonitor {
SystemObserver::MetricRefreshFrequencies metrics_frequencies); SystemObserver::MetricRefreshFrequencies metrics_frequencies);
// Removes |observer| from the observer list. After this call, the observer // Removes |observer| from the observer list. After this call, the observer
// will not receive notifications for any metric. // will not receive notifications for any metric. This is a no-op if this
// observer isn't registred.
void RemoveObserver(SystemObserver* observer); void RemoveObserver(SystemObserver* observer);
const base::OneShotTimer& refresh_timer_for_testing() { const base::OneShotTimer& refresh_timer_for_testing() {
......
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