diff options
author | Berthold Stoeger <bstoeger@mail.tuwien.ac.at> | 2019-04-26 18:03:23 +0200 |
---|---|---|
committer | Dirk Hohndel <dirk@hohndel.org> | 2019-04-29 13:03:31 -0700 |
commit | d498fcad61253591c653e6285173b653483926bf (patch) | |
tree | 9b8b7f338b00376f7c636f778509003d86d7a5a7 /desktop-widgets/templatelayout.cpp | |
parent | af00361929c00febea42d067785ea1a76c493e7b (diff) | |
download | subsurface-d498fcad61253591c653e6285173b653483926bf.tar.gz |
Printing: fix memory leak
For printing, DiveObjectHelpers are allocated and pointers to these
are stored in a QVariantList. The objects are never freed. To fix
this leak, keep the objects in a std::list<>.
std::list<> was chosen because
1) Pointers to elements stay valid during its lifetime.
2) Objects can be constructed directly in the list with emplace_back()
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
Diffstat (limited to 'desktop-widgets/templatelayout.cpp')
-rw-r--r-- | desktop-widgets/templatelayout.cpp | 15 |
1 files changed, 11 insertions, 4 deletions
diff --git a/desktop-widgets/templatelayout.cpp b/desktop-widgets/templatelayout.cpp index 27afbac65..06039dbf2 100644 --- a/desktop-widgets/templatelayout.cpp +++ b/desktop-widgets/templatelayout.cpp @@ -1,6 +1,7 @@ // SPDX-License-Identifier: GPL-2.0 #include <QFileDevice> #include <string> +#include <list> #include "templatelayout.h" #include "core/display.h" @@ -141,12 +142,18 @@ QString TemplateLayout::generate() Grantlee::registerMetaType<template_options>(); Grantlee::registerMetaType<print_options>(); + // Note: Currently, this should not be transformed into a QVector<> or std::vector<>, + // as diveList contains pointers to elements in this list. But vectors might relocate + // and thus invalidate the pointers! std::list<> is used here, because the new elements + // can be directly constructed in the list with the emplace_back() call. + // Ultimately, the memory management should be fixed. + std::list<DiveObjectHelper> diveObjectList; QVariantList diveList; struct dive *dive; if (in_planner()) { - DiveObjectHelper *d = new DiveObjectHelper(&displayed_dive); - diveList.append(QVariant::fromValue(d)); + diveObjectList.emplace_back(&displayed_dive); + diveList.append(QVariant::fromValue(&diveObjectList.back())); emit progressUpdated(100.0); } else { int i; @@ -154,8 +161,8 @@ QString TemplateLayout::generate() //TODO check for exporting selected dives only if (!dive->selected && printOptions->print_selected) continue; - DiveObjectHelper *d = new DiveObjectHelper(dive); - diveList.append(QVariant::fromValue(d)); + diveObjectList.emplace_back(dive); + diveList.append(QVariant::fromValue(&diveObjectList.back())); progress++; emit progressUpdated(lrint(progress * 100.0 / totalWork)); } |