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>
This commit is contained in:
Berthold Stoeger 2019-04-26 18:03:23 +02:00 committed by Dirk Hohndel
parent af00361929
commit d498fcad61

View file

@ -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));
}