From c49d605320e9464939b507182fddc3cd5993d4d3 Mon Sep 17 00:00:00 2001 From: Dean Lee Date: Sat, 1 Jul 2023 16:45:05 +0800 Subject: [PATCH] UI: fix memory leak in MapSettings (#28759) Co-authored-by: Cameron Clough old-commit-hash: 93413c43df594a63b36c896c629daaef27dd9fe4 --- selfdrive/ui/qt/maps/map_settings.cc | 24 ++++++++++++------------ selfdrive/ui/qt/maps/map_settings.h | 4 +++- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/selfdrive/ui/qt/maps/map_settings.cc b/selfdrive/ui/qt/maps/map_settings.cc index c4f205dcb7..674f060120 100644 --- a/selfdrive/ui/qt/maps/map_settings.cc +++ b/selfdrive/ui/qt/maps/map_settings.cc @@ -131,12 +131,12 @@ void MapSettings::updateCurrentRoute() { qWarning() << "JSON Parse failed on NavDestination" << dest; return; } - auto destination = new NavDestination(doc.object()); + auto destination = std::make_unique(doc.object()); if (current_destination && *destination == *current_destination) return; - current_destination = destination; - current_widget->set(current_destination, true); + current_destination = std::move(destination); + current_widget->set(current_destination.get(), true); } else { - current_destination = nullptr; + current_destination.reset(nullptr); current_widget->unset("", true); } if (isVisible()) refresh(); @@ -150,7 +150,7 @@ void MapSettings::parseResponse(const QString &response, bool success) { void MapSettings::refresh() { bool has_home = false, has_work = false; - auto destinations = std::vector(); + auto destinations = std::vector>(); auto destinations_str = cur_destinations.trimmed(); if (!destinations_str.isEmpty()) { @@ -161,7 +161,7 @@ void MapSettings::refresh() { } for (auto el : doc.array()) { - auto destination = new NavDestination(el.toObject()); + auto destination = std::make_unique(el.toObject()); // add home and work later if they are missing if (destination->isFavorite()) { @@ -171,7 +171,7 @@ void MapSettings::refresh() { // skip current destination if (current_destination && *destination == *current_destination) continue; - destinations.push_back(destination); + destinations.push_back(std::move(destination)); } } @@ -179,7 +179,7 @@ void MapSettings::refresh() { clearLayout(destinations_layout); // Sort: HOME, WORK, and then descending-alphabetical FAVORITES, RECENTS - std::sort(destinations.begin(), destinations.end(), [](const NavDestination *a, const NavDestination *b) { + std::sort(destinations.begin(), destinations.end(), [](const auto &a, const auto &b) { if (a->isFavorite() && b->isFavorite()) { if (a->label() == NAV_FAVORITE_LABEL_HOME) return true; else if (b->label() == NAV_FAVORITE_LABEL_HOME) return false; @@ -192,12 +192,12 @@ void MapSettings::refresh() { return a->name() < b->name(); }); - for (auto destination : destinations) { + for (auto &destination : destinations) { auto widget = new DestinationWidget(this); - widget->set(destination, false); + widget->set(destination.get(), false); - QObject::connect(widget, &QPushButton::clicked, [=]() { - navigateTo(destination->toJson()); + QObject::connect(widget, &QPushButton::clicked, [this, dest = destination->toJson()]() { + navigateTo(dest); emit closeSettings(); }); diff --git a/selfdrive/ui/qt/maps/map_settings.h b/selfdrive/ui/qt/maps/map_settings.h index 38c021036c..39fbe740f4 100644 --- a/selfdrive/ui/qt/maps/map_settings.h +++ b/selfdrive/ui/qt/maps/map_settings.h @@ -1,5 +1,7 @@ #pragma once +#include + #include #include #include @@ -37,7 +39,7 @@ private: Params params; QString cur_destinations; QVBoxLayout *destinations_layout; - NavDestination *current_destination; + std::unique_ptr current_destination; DestinationWidget *current_widget; QPixmap close_icon;