From 9284bfec9b6a0ab00b6eabddc1fc47dfb920ae76 Mon Sep 17 00:00:00 2001 From: Roman Telezhynskyi Date: Fri, 14 Apr 2017 11:30:00 +0300 Subject: [PATCH] Use smart pointer to prevent memory leak. --HG-- branch : release --- src/libs/vformat/vmeasurements.cpp | 25 ++++++++++++------------- src/libs/vpatterndb/vcontainer.h | 11 ++++++++++- 2 files changed, 22 insertions(+), 14 deletions(-) diff --git a/src/libs/vformat/vmeasurements.cpp b/src/libs/vformat/vmeasurements.cpp index b7639ba0b..3bd631ad2 100644 --- a/src/libs/vformat/vmeasurements.cpp +++ b/src/libs/vformat/vmeasurements.cpp @@ -231,7 +231,7 @@ void VMeasurements::ReadMeasurements() const // That's why we need two containers: one for converted values, second for real data. // Container for values in measurement file's unit - VContainer *tempData = new VContainer(data->GetTrVars(), data->GetPatternUnit()); + QScopedPointer tempData(new VContainer(data->GetTrVars(), data->GetPatternUnit())); const QDomNodeList list = elementsByTagName(TagMeasurement); for (int i=0; i < list.size(); ++i) @@ -260,16 +260,16 @@ void VMeasurements::ReadMeasurements() const Q_UNUSED(e) } - VMeasurement *meash; - VMeasurement *tempMeash; + QSharedPointer meash; + QSharedPointer tempMeash; if (type == MeasurementsType::Standard) { qreal base = GetParametrDouble(dom, AttrBase, "0"); qreal ksize = GetParametrDouble(dom, AttrSizeIncrease, "0"); qreal kheight = GetParametrDouble(dom, AttrHeightIncrease, "0"); - tempMeash = new VMeasurement(static_cast(i), name, BaseSize(), BaseHeight(), base, ksize, - kheight); + tempMeash = QSharedPointer(new VMeasurement(static_cast(i), name, BaseSize(), + BaseHeight(), base, ksize, kheight)); base = UnitConvertor(base, MUnit(), *data->GetPatternUnit()); ksize = UnitConvertor(ksize, MUnit(), *data->GetPatternUnit()); @@ -278,26 +278,25 @@ void VMeasurements::ReadMeasurements() const const qreal baseSize = UnitConvertor(BaseSize(), MUnit(), *data->GetPatternUnit()); const qreal baseHeight = UnitConvertor(BaseHeight(), MUnit(), *data->GetPatternUnit()); - meash = new VMeasurement(static_cast(i), name, baseSize, baseHeight, base, ksize, kheight, - fullName, description); + meash = QSharedPointer(new VMeasurement(static_cast(i), name, baseSize, baseHeight, + base, ksize, kheight, fullName, description)); } else { const QString formula = GetParametrString(dom, AttrValue, "0"); bool ok = false; - qreal value = EvalFormula(tempData, formula, &ok); + qreal value = EvalFormula(tempData.data(), formula, &ok); - tempMeash = new VMeasurement(tempData, static_cast(i), name, value, formula, ok); + tempMeash = QSharedPointer(new VMeasurement(tempData.data(), static_cast(i), name, + value, formula, ok)); value = UnitConvertor(value, MUnit(), *data->GetPatternUnit()); - meash = new VMeasurement(data, static_cast(i), name, value, formula, ok, fullName, - description); + meash = QSharedPointer(new VMeasurement(data, static_cast(i), name, value, formula, + ok, fullName, description)); } tempData->AddVariable(name, tempMeash); data->AddVariable(name, meash); } - - delete tempData; } //--------------------------------------------------------------------------------------------------------------------- diff --git a/src/libs/vpatterndb/vcontainer.h b/src/libs/vpatterndb/vcontainer.h index a049cc58a..2c4221f9f 100644 --- a/src/libs/vpatterndb/vcontainer.h +++ b/src/libs/vpatterndb/vcontainer.h @@ -147,6 +147,8 @@ public: template void AddVariable(const QString& name, T *var); + template + void AddVariable(const QString& name, const QSharedPointer &var); void RemoveVariable(const QString& name); void RemovePiece(quint32 id); @@ -287,6 +289,13 @@ QSharedPointer VContainer::GetVariable(QString name) const //--------------------------------------------------------------------------------------------------------------------- template void VContainer::AddVariable(const QString& name, T *var) +{ + AddVariable(name, QSharedPointer(var)); +} + +//--------------------------------------------------------------------------------------------------------------------- +template +void VContainer::AddVariable(const QString& name, const QSharedPointer &var) { if (d->variables.contains(name)) { @@ -299,7 +308,7 @@ void VContainer::AddVariable(const QString& name, T *var) throw VExceptionBadId(tr("Can't find object. Type mismatch."), name); } } - d->variables.insert(name, QSharedPointer(var)); + d->variables.insert(name, var); uniqueNames.insert(name); }