Use smart pointer to prevent memory leak.

--HG--
branch : release
This commit is contained in:
Roman Telezhynskyi 2017-04-14 11:30:00 +03:00
parent 1e06446189
commit 9284bfec9b
2 changed files with 22 additions and 14 deletions

View File

@ -231,7 +231,7 @@ void VMeasurements::ReadMeasurements() const
// That's why we need two containers: one for converted values, second for real data. // That's why we need two containers: one for converted values, second for real data.
// Container for values in measurement file's unit // Container for values in measurement file's unit
VContainer *tempData = new VContainer(data->GetTrVars(), data->GetPatternUnit()); QScopedPointer<VContainer> tempData(new VContainer(data->GetTrVars(), data->GetPatternUnit()));
const QDomNodeList list = elementsByTagName(TagMeasurement); const QDomNodeList list = elementsByTagName(TagMeasurement);
for (int i=0; i < list.size(); ++i) for (int i=0; i < list.size(); ++i)
@ -260,16 +260,16 @@ void VMeasurements::ReadMeasurements() const
Q_UNUSED(e) Q_UNUSED(e)
} }
VMeasurement *meash; QSharedPointer<VMeasurement> meash;
VMeasurement *tempMeash; QSharedPointer<VMeasurement> tempMeash;
if (type == MeasurementsType::Standard) if (type == MeasurementsType::Standard)
{ {
qreal base = GetParametrDouble(dom, AttrBase, "0"); qreal base = GetParametrDouble(dom, AttrBase, "0");
qreal ksize = GetParametrDouble(dom, AttrSizeIncrease, "0"); qreal ksize = GetParametrDouble(dom, AttrSizeIncrease, "0");
qreal kheight = GetParametrDouble(dom, AttrHeightIncrease, "0"); qreal kheight = GetParametrDouble(dom, AttrHeightIncrease, "0");
tempMeash = new VMeasurement(static_cast<quint32>(i), name, BaseSize(), BaseHeight(), base, ksize, tempMeash = QSharedPointer<VMeasurement>(new VMeasurement(static_cast<quint32>(i), name, BaseSize(),
kheight); BaseHeight(), base, ksize, kheight));
base = UnitConvertor(base, MUnit(), *data->GetPatternUnit()); base = UnitConvertor(base, MUnit(), *data->GetPatternUnit());
ksize = UnitConvertor(ksize, 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 baseSize = UnitConvertor(BaseSize(), MUnit(), *data->GetPatternUnit());
const qreal baseHeight = UnitConvertor(BaseHeight(), MUnit(), *data->GetPatternUnit()); const qreal baseHeight = UnitConvertor(BaseHeight(), MUnit(), *data->GetPatternUnit());
meash = new VMeasurement(static_cast<quint32>(i), name, baseSize, baseHeight, base, ksize, kheight, meash = QSharedPointer<VMeasurement>(new VMeasurement(static_cast<quint32>(i), name, baseSize, baseHeight,
fullName, description); base, ksize, kheight, fullName, description));
} }
else else
{ {
const QString formula = GetParametrString(dom, AttrValue, "0"); const QString formula = GetParametrString(dom, AttrValue, "0");
bool ok = false; bool ok = false;
qreal value = EvalFormula(tempData, formula, &ok); qreal value = EvalFormula(tempData.data(), formula, &ok);
tempMeash = new VMeasurement(tempData, static_cast<quint32>(i), name, value, formula, ok); tempMeash = QSharedPointer<VMeasurement>(new VMeasurement(tempData.data(), static_cast<quint32>(i), name,
value, formula, ok));
value = UnitConvertor(value, MUnit(), *data->GetPatternUnit()); value = UnitConvertor(value, MUnit(), *data->GetPatternUnit());
meash = new VMeasurement(data, static_cast<quint32>(i), name, value, formula, ok, fullName, meash = QSharedPointer<VMeasurement>(new VMeasurement(data, static_cast<quint32>(i), name, value, formula,
description); ok, fullName, description));
} }
tempData->AddVariable(name, tempMeash); tempData->AddVariable(name, tempMeash);
data->AddVariable(name, meash); data->AddVariable(name, meash);
} }
delete tempData;
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------

View File

@ -147,6 +147,8 @@ public:
template <typename T> template <typename T>
void AddVariable(const QString& name, T *var); void AddVariable(const QString& name, T *var);
template <typename T>
void AddVariable(const QString& name, const QSharedPointer<T> &var);
void RemoveVariable(const QString& name); void RemoveVariable(const QString& name);
void RemovePiece(quint32 id); void RemovePiece(quint32 id);
@ -287,6 +289,13 @@ QSharedPointer<T> VContainer::GetVariable(QString name) const
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
template <typename T> template <typename T>
void VContainer::AddVariable(const QString& name, T *var) void VContainer::AddVariable(const QString& name, T *var)
{
AddVariable(name, QSharedPointer<T>(var));
}
//---------------------------------------------------------------------------------------------------------------------
template <typename T>
void VContainer::AddVariable(const QString& name, const QSharedPointer<T> &var)
{ {
if (d->variables.contains(name)) 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); throw VExceptionBadId(tr("Can't find object. Type mismatch."), name);
} }
} }
d->variables.insert(name, QSharedPointer<T>(var)); d->variables.insert(name, var);
uniqueNames.insert(name); uniqueNames.insert(name);
} }