Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix] Remaining memory leaks in tests #5494

Merged
merged 3 commits into from Sep 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -232,7 +232,7 @@ namespace OpenMS

@return A pointer to the trace fitter that should be used.
*/
TraceFitter* chooseTraceFitter_(double& tau);
std::unique_ptr<TraceFitter> chooseTraceFitter_(double& tau);

double intensityScore_(Size rt_bin, Size mz_bin, double intensity) const;

Expand All @@ -251,7 +251,7 @@ namespace OpenMS
@param traces Original mass traces found in the experiment.
@param new_traces Mass traces created by cropping the original mass traces.
*/
void cropFeature_(TraceFitter* fitter,
void cropFeature_(std::shared_ptr<TraceFitter> fitter,
const MassTraces& traces,
MassTraces& new_traces);

Expand All @@ -278,7 +278,7 @@ namespace OpenMS

@return true if the feature is valid
*/
bool checkFeatureQuality_(TraceFitter* fitter,
bool checkFeatureQuality_(std::shared_ptr<TraceFitter> fitter,
MassTraces& feature_traces,
const double& seed_mz, const double& min_feature_score,
String& error_msg, double& fit_score, double& correlation, double& final_score);
Expand All @@ -296,7 +296,7 @@ namespace OpenMS
@param peak The Seed Peak
@param path The path where to put the debug files (default is debug/features)
*/
void writeFeatureDebugInfo_(TraceFitter* fitter,
void writeFeatureDebugInfo_(std::shared_ptr<TraceFitter> fitter,
const MassTraces& traces,
const MassTraces& new_traces,
bool feature_ok, const String error_msg, const double final_score, const Int plot_nr, const PeakType& peak,
Expand Down
16 changes: 12 additions & 4 deletions src/openms/source/CHEMISTRY/TheoreticalSpectrumGenerator.cpp
Expand Up @@ -41,6 +41,7 @@
#include <OpenMS/CHEMISTRY/AASequence.h>
#include <OpenMS/CHEMISTRY/ResidueDB.h>
#include <OpenMS/KERNEL/MSSpectrum.h>
#include <OpenMS/CONCEPT/RAIICleanup.h>

#include <unordered_set>

Expand Down Expand Up @@ -144,7 +145,17 @@ namespace OpenMS
PeakSpectrum::StringDataArray* ion_names;
PeakSpectrum::IntegerDataArray* charges;

bool charges_dynamic = false, ion_names_dynamic = false;
bool charges_dynamic = false;
bool ion_names_dynamic = false;

// Assure memory is freed even if an exception occurs.
RAIICleanup _(
[&]
{
if (charges_dynamic) delete charges;
if (ion_names_dynamic) delete ion_names;
}
);

if (spectrum.getIntegerDataArrays().empty())
{
Expand Down Expand Up @@ -212,9 +223,6 @@ namespace OpenMS
}
}

if (charges_dynamic) delete charges;
if (ion_names_dynamic) delete ion_names;

if (sort_by_position_) spectrum.sortByPositionPresorted(chunks.getChunks());

// set MS Level
Expand Down
4 changes: 1 addition & 3 deletions src/openms/source/METADATA/PeptideHit.cpp
Expand Up @@ -126,11 +126,9 @@ namespace OpenMS
MetaInfoInterface::operator=(source);
sequence_ = source.sequence_;
score_ = source.score_;
analysis_results_ = nullptr;
delete analysis_results_;
if (source.analysis_results_ != nullptr)
{
// free memory first
delete analysis_results_;
analysis_results_ = new std::vector<PepXMLAnalysisResult>(*source.analysis_results_);
}
rank_ = source.rank_;
Expand Down
Expand Up @@ -678,7 +678,7 @@ namespace OpenMS

// choose fitter
double egh_tau = 0.0;
TraceFitter* fitter = chooseTraceFitter_(egh_tau);
std::shared_ptr<TraceFitter> fitter = chooseTraceFitter_(egh_tau);

fitter->setParameters(trace_fitter_params);
fitter->fit(traces);
Expand Down Expand Up @@ -756,10 +756,10 @@ namespace OpenMS
// Extract some of the model parameters.
if (egh_tau != 0.0)
{
egh_tau = (static_cast<EGHTraceFitter*>(fitter))->getTau();
egh_tau = (std::dynamic_pointer_cast<EGHTraceFitter>(fitter))->getTau();
f.setMetaValue("EGH_tau", egh_tau);
f.setMetaValue("EGH_height", (static_cast<EGHTraceFitter*>(fitter))->getHeight());
f.setMetaValue("EGH_sigma", (static_cast<EGHTraceFitter*>(fitter))->getSigma());
f.setMetaValue("EGH_height", (std::dynamic_pointer_cast<EGHTraceFitter>(fitter))->getHeight());
f.setMetaValue("EGH_sigma", (std::dynamic_pointer_cast<EGHTraceFitter>(fitter))->getSigma());
}

// Calculate the mass of the feature: maximum, average, monoisotopic
Expand Down Expand Up @@ -794,9 +794,6 @@ namespace OpenMS
// - as we scaled the isotope distribution to
f.setIntensity(fitter->getArea() / getIsotopeDistribution_(f.getMZ()).max);

// we do not need the fitter anymore
delete fitter;

//add convex hulls of mass traces
for (Size j = 0; j < traces.size(); ++j)
{
Expand Down Expand Up @@ -1696,19 +1693,19 @@ namespace OpenMS
return final;
}

TraceFitter* FeatureFinderAlgorithmPicked::chooseTraceFitter_(double& tau)
std::unique_ptr<TraceFitter> FeatureFinderAlgorithmPicked::chooseTraceFitter_(double& tau)
{
// choose fitter
if (param_.getValue("feature:rt_shape") == "asymmetric")
{
OPENMS_LOG_DEBUG << "use asymmetric rt peak shape" << std::endl;
tau = -1.0;
return new EGHTraceFitter();
return std::make_unique<EGHTraceFitter>();
}
else // if (param_.getValue("feature:rt_shape") == "symmetric")
{
OPENMS_LOG_DEBUG << "use symmetric rt peak shape" << std::endl;
return new GaussTraceFitter();
return std::make_unique<GaussTraceFitter>();
}
}

Expand Down Expand Up @@ -1746,7 +1743,7 @@ namespace OpenMS
return final;
}

void FeatureFinderAlgorithmPicked::cropFeature_(TraceFitter* fitter,
void FeatureFinderAlgorithmPicked::cropFeature_(std::shared_ptr<TraceFitter> fitter,
const MassTraces& traces,
MassTraces& new_traces)
{
Expand Down Expand Up @@ -1825,7 +1822,7 @@ namespace OpenMS
new_traces.baseline = traces.baseline;
}

bool FeatureFinderAlgorithmPicked::checkFeatureQuality_(TraceFitter* fitter,
bool FeatureFinderAlgorithmPicked::checkFeatureQuality_(std::shared_ptr<TraceFitter> fitter,
MassTraces& feature_traces,
const double& seed_mz, const double& min_feature_score,
String& error_msg, double& fit_score, double& correlation, double& final_score)
Expand Down Expand Up @@ -1907,7 +1904,7 @@ namespace OpenMS
return true;
}

void FeatureFinderAlgorithmPicked::writeFeatureDebugInfo_(TraceFitter* fitter,
void FeatureFinderAlgorithmPicked::writeFeatureDebugInfo_(std::shared_ptr<TraceFitter> fitter,
const MassTraces& traces,
const MassTraces& new_traces,
bool feature_ok, const String error_msg, const double final_score, const Int plot_nr, const PeakType& peak,
Expand Down