mirror of
https://github.com/subsurface/subsurface.git
synced 2025-02-19 22:16:15 +00:00
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:
parent
af00361929
commit
d498fcad61
1 changed files with 11 additions and 4 deletions
|
@ -1,6 +1,7 @@
|
||||||
// SPDX-License-Identifier: GPL-2.0
|
// SPDX-License-Identifier: GPL-2.0
|
||||||
#include <QFileDevice>
|
#include <QFileDevice>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <list>
|
||||||
|
|
||||||
#include "templatelayout.h"
|
#include "templatelayout.h"
|
||||||
#include "core/display.h"
|
#include "core/display.h"
|
||||||
|
@ -141,12 +142,18 @@ QString TemplateLayout::generate()
|
||||||
Grantlee::registerMetaType<template_options>();
|
Grantlee::registerMetaType<template_options>();
|
||||||
Grantlee::registerMetaType<print_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;
|
QVariantList diveList;
|
||||||
|
|
||||||
struct dive *dive;
|
struct dive *dive;
|
||||||
if (in_planner()) {
|
if (in_planner()) {
|
||||||
DiveObjectHelper *d = new DiveObjectHelper(&displayed_dive);
|
diveObjectList.emplace_back(&displayed_dive);
|
||||||
diveList.append(QVariant::fromValue(d));
|
diveList.append(QVariant::fromValue(&diveObjectList.back()));
|
||||||
emit progressUpdated(100.0);
|
emit progressUpdated(100.0);
|
||||||
} else {
|
} else {
|
||||||
int i;
|
int i;
|
||||||
|
@ -154,8 +161,8 @@ QString TemplateLayout::generate()
|
||||||
//TODO check for exporting selected dives only
|
//TODO check for exporting selected dives only
|
||||||
if (!dive->selected && printOptions->print_selected)
|
if (!dive->selected && printOptions->print_selected)
|
||||||
continue;
|
continue;
|
||||||
DiveObjectHelper *d = new DiveObjectHelper(dive);
|
diveObjectList.emplace_back(dive);
|
||||||
diveList.append(QVariant::fromValue(d));
|
diveList.append(QVariant::fromValue(&diveObjectList.back()));
|
||||||
progress++;
|
progress++;
|
||||||
emit progressUpdated(lrint(progress * 100.0 / totalWork));
|
emit progressUpdated(lrint(progress * 100.0 / totalWork));
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue