Skip to content

Commit 2b876bb

Browse files
committed
[RF] Fix various memory leaks caused by not deleting return values
Fix various memory leaks from cases where functions that return an owned return value were used without deleting the return value. This is addressed by using `std::unique_ptr`.
1 parent d536fdf commit 2b876bb

15 files changed

Lines changed: 34 additions & 28 deletions

roofit/histfactory/src/HistFactoryModelUtils.cxx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ namespace HistFactory{
5656
if( ! FoundSumPdf ) {
5757
if(verbose) {
5858
std::cout << "Failed to find RooRealSumPdf for channel: " << sim_channel->GetName() << std::endl;
59-
sim_channel->getComponents()->Print("V");
59+
components->Print("V");
6060
}
6161
sum_pdf=nullptr;
6262
//throw std::runtime_error("Failed to find RooRealSumPdf for channel");

roofit/roofit/test/testFitPerf.cxx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -549,7 +549,7 @@ int FitUsingRooFit(TTree *tree, TF1 *func)
549549
int level = 3;
550550
std::cout << "num entries = " << data.numEntries() << std::endl;
551551
bool save = true;
552-
(pdf.getVariables())->Print("v"); // print the parameters
552+
std::unique_ptr<RooArgSet>{pdf.getVariables()}->Print("v"); // print the parameters
553553
#else
554554
int level = -1;
555555
bool save = false;
@@ -630,7 +630,7 @@ int FitUsingRooFit2(TTree *tree)
630630
int level = 3;
631631
std::cout << "num entries = " << data.numEntries() << std::endl;
632632
bool save = true;
633-
(pdf[N - 1]->getVariables())->Print("v"); // print the parameters
633+
std::unique_ptr<RooArgSet>{pdf[N - 1]->getVariables()}->Print("v"); // print the parameters
634634
std::cout << "\n\nDo the fit now \n\n";
635635
#else
636636
int level = -1;

roofit/roofit/test/vectorisedPDFs/VectorisedPDFTests.cxx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ std::unique_ptr<RooFitResult> PDFTest::runBatchFit(RooAbsPdf *pdf)
457457
kickParameters();
458458
makePlots(::testing::UnitTest::GetInstance()->current_test_info()->name() + std::string("_batch_prefit"));
459459

460-
auto pars = pdf->getParameters(*_dataFit);
460+
std::unique_ptr<RooArgSet> pars{pdf->getParameters(*_dataFit)};
461461
*pars = _parameters;
462462

463463
for (unsigned int index = 0; index < pars->size(); ++index) {

roofit/roofitcore/src/RooAbsReal.cxx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2247,10 +2247,10 @@ RooPlot* RooAbsReal::plotAsymOn(RooPlot *frame, const RooAbsCategoryLValue& asym
22472247

22482248

22492249
// Take out data-projected dependents from projectedVars
2250-
RooArgSet* projDataNeededVars = nullptr ;
2250+
std::unique_ptr<RooArgSet> projDataNeededVars;
22512251
if (o.projData) {
2252-
projDataNeededVars = projectedVars.selectCommon(projDataVars);
2253-
projectedVars.remove(projDataVars,true,true) ;
2252+
projDataNeededVars.reset(projectedVars.selectCommon(projDataVars));
2253+
projectedVars.remove(projDataVars, true, true);
22542254
}
22552255

22562256
// Take out plotted asymmetry from projection

roofit/roofitcore/src/RooRealMPFE.cxx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ For general multiprocessing in ROOT, please refer to the TProcessExecutor class.
5353
#endif
5454

5555
#include <cstdlib>
56+
#include <memory>
5657
#include <sstream>
5758
#include "RooRealMPFE.h"
5859
#include "RooArgSet.h"
@@ -170,7 +171,7 @@ void RooRealMPFE::initVars()
170171
_saveVars.removeAll() ;
171172

172173
// Retrieve non-constant parameters
173-
auto vars = _arg->getParameters(RooArgSet());
174+
std::unique_ptr<RooArgSet> vars{_arg->getParameters(RooArgSet())};
174175
// RooArgSet *ncVars = vars->selectByAttrib("Constant", false);
175176
RooArgList varList(*vars) ;
176177

roofit/roofitcore/src/RooVectorDataStore.cxx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -829,6 +829,7 @@ void RooVectorDataStore::cacheArgs(const RooAbsArg* owner, RooArgSet& newVarSet,
829829

830830
std::vector<RooArgSet*> nsetList ;
831831
std::vector<std::unique_ptr<RooArgSet>> argObsList ;
832+
std::vector<std::unique_ptr<RooArgSet>> ownedNsets;
832833

833834
// Now need to attach branch buffers of clones
834835
for (const auto arg : cloneSet) {
@@ -844,8 +845,8 @@ void RooVectorDataStore::cacheArgs(const RooAbsArg* owner, RooArgSet& newVarSet,
844845
// std::cout << "RooVectorDataStore::cacheArgs() cached node " << arg->GetName() << " has a normalization set specification CATNormSet = " << catNset << std::endl ;
845846

846847
RooArgSet anset = RooHelpers::selectFromArgSet(nset ? *nset : RooArgSet{}, catNset);
847-
normSet = anset.selectCommon(*argObs);
848-
848+
ownedNsets.emplace_back(anset.selectCommon(*argObs));
849+
normSet = ownedNsets.back().get();
849850
}
850851
const char* catCset = arg->getStringAttribute("CATCondSet") ;
851852
if (catCset) {

roofit/roofitcore/src/TestStatistics/RooAbsL.cxx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ void RooAbsL::initClones(RooAbsPdf &inpdf, RooAbsData &indata)
129129
// ******************************************************************
130130

131131
// Attach FUNC to data set
132-
auto _funcObsSet = pdf_->getObservables(indata);
132+
std::unique_ptr<RooArgSet> _funcObsSet{pdf_->getObservables(indata)};
133133

134134
if (pdf_->getAttribute("BinnedLikelihood")) {
135135
pdf_->setAttribute("BinnedLikelihoodActive");

roofit/roostats/src/AsymptoticCalculator.cxx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,8 +245,8 @@ bool AsymptoticCalculator::Initialize() const {
245245
if (!fNominalAsimov) {
246246
if (verbose >= 0)
247247
oocoutI(nullptr,InputArguments) << "AsymptoticCalculator: Asimov data will be generated using fitted nuisance parameter values" << std::endl;
248-
RooArgSet * tmp = (RooArgSet*) poiAlt.snapshot();
249-
fAsimovData = MakeAsimovData( data, *GetNullModel(), poiAlt, fAsimovGlobObs,tmp);
248+
std::unique_ptr<RooArgSet> tmp{static_cast<RooArgSet *>(poiAlt.snapshot())};
249+
fAsimovData = MakeAsimovData(data, *GetNullModel(), poiAlt, fAsimovGlobObs, tmp.get());
250250
}
251251

252252
else {

roofit/roostats/src/HypoTestInverter.cxx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1237,7 +1237,8 @@ SamplingDistribution * HypoTestInverter::RebuildDistributions(bool isUpper, int
12371237
inverter.SetData(*bkgdata);
12381238

12391239
// print global observables
1240-
auto gobs = bModel->GetPdf()->getVariables()->selectCommon(* sbModel->GetGlobalObservables() );
1240+
std::unique_ptr<RooArgSet> bModelVars{bModel->GetPdf()->getVariables()};
1241+
std::unique_ptr<RooAbsCollection> gobs{bModelVars->selectCommon(*sbModel->GetGlobalObservables())};
12411242
gobs->Print("v");
12421243

12431244
HypoTestInverterResult * r = inverter.GetInterval();

roofit/roostats/src/LikelihoodIntervalPlot.cxx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,8 @@ void LikelihoodIntervalPlot::Draw(const Option_t *options)
194194
const double xcont_min = fInterval->LowerLimit(*myparam);
195195
const double xcont_max = fInterval->UpperLimit(*myparam);
196196

197-
RooRealVar* myarg = static_cast<RooRealVar *>(newProfile->getVariables()->find(myparam->GetName()));
197+
std::unique_ptr<RooArgSet> vars{newProfile->getVariables()};
198+
RooRealVar *myarg = static_cast<RooRealVar *>(vars->find(myparam->GetName()));
198199
double x1 = myarg->getMin();
199200
double x2 = myarg->getMax();
200201

@@ -336,7 +337,8 @@ void LikelihoodIntervalPlot::Draw(const Option_t *options)
336337
double cont_level = ROOT::Math::chisquared_quantile(fInterval->ConfidenceLevel(),fNdimPlot); // level for -2log LR
337338
cont_level = cont_level/2; // since we are plotting -log LR
338339

339-
RooArgList params(*newProfile->getVariables());
340+
std::unique_ptr<RooArgSet> vars{newProfile->getVariables()};
341+
RooArgList params(*vars);
340342
// set values and error for the POI to the best fit values
341343
for (std::size_t i = 0; i < params.size(); ++i) {
342344
RooRealVar & par = static_cast<RooRealVar &>( params[i]);

0 commit comments

Comments
 (0)