Skip to content

Commit 0287b03

Browse files
committed
use smart pointers to avoid potential memory leak
If Configure() is called multiple times on the same GRV98LO instance, the `new`s in Intitialize() will leak memory. This can be cleanly avoided by using smart pointers instead of direct allocation.
1 parent 1ee1e67 commit 0287b03

File tree

2 files changed

+17
-34
lines changed

2 files changed

+17
-34
lines changed

src/Physics/PartonDistributions/GRV98LO.cxx

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -23,40 +23,21 @@ using namespace genie;
2323

2424
//____________________________________________________________________________
2525
GRV98LO::GRV98LO() :
26-
PDFModelI("genie::GRV98LO"),
27-
fXUVF(NULL),
28-
fXDVF(NULL),
29-
fXDEF(NULL),
30-
fXUDF(NULL),
31-
fXSF (NULL),
32-
fXGF (NULL)
26+
PDFModelI("genie::GRV98LO")
3327
{
3428
this->Initialize();
3529
}
3630
//____________________________________________________________________________
3731
GRV98LO::GRV98LO(string config) :
38-
PDFModelI("genie::GRV98LO", config),
39-
fXUVF(NULL),
40-
fXDVF(NULL),
41-
fXDEF(NULL),
42-
fXUDF(NULL),
43-
fXSF (NULL),
44-
fXGF (NULL)
32+
PDFModelI("genie::GRV98LO", config)
4533
{
4634
LOG("GRV98LO", pDEBUG) << "GRV98LO configuration:\n " << GetConfig() ;
4735

4836
this->Initialize();
4937
}
5038
//____________________________________________________________________________
5139
GRV98LO::~GRV98LO()
52-
{
53-
if (fXUVF) {delete fXUVF; fXUVF = NULL;}
54-
if (fXDVF) {delete fXDVF; fXDVF = NULL;}
55-
if (fXDEF) {delete fXDEF; fXDEF = NULL;}
56-
if (fXUDF) {delete fXUDF; fXUDF = NULL;}
57-
if (fXSF ) {delete fXSF ; fXSF = NULL;}
58-
if (fXGF ) {delete fXGF ; fXGF = NULL;}
59-
}
40+
{}
6041
//____________________________________________________________________________
6142
double GRV98LO::UpValence(double x, double Q2) const
6243
{
@@ -330,12 +311,12 @@ void GRV98LO::Initialize(void)
330311
k++;
331312
}
332313

333-
fXUVF = new Interpolator2D(gridLogXbj.size(),&gridLogXbj[0],gridLogQ2.size(),&gridLogQ2[0],&knotsXUVF[0]);
334-
fXDVF = new Interpolator2D(gridLogXbj.size(),&gridLogXbj[0],gridLogQ2.size(),&gridLogQ2[0],&knotsXDVF[0]);
335-
fXDEF = new Interpolator2D(gridLogXbj.size(),&gridLogXbj[0],gridLogQ2.size(),&gridLogQ2[0],&knotsXDEF[0]);
336-
fXUDF = new Interpolator2D(gridLogXbj.size(),&gridLogXbj[0],gridLogQ2.size(),&gridLogQ2[0],&knotsXUDF[0]);
337-
fXSF = new Interpolator2D(gridLogXbj.size(),&gridLogXbj[0],gridLogQ2.size(),&gridLogQ2[0],&knotsXSF [0]);
338-
fXGF = new Interpolator2D(gridLogXbj.size(),&gridLogXbj[0],gridLogQ2.size(),&gridLogQ2[0],&knotsXGF [0]);
314+
fXUVF = std::make_unique<Interpolator2D>(gridLogXbj.size(),&gridLogXbj[0],gridLogQ2.size(),&gridLogQ2[0],&knotsXUVF[0]);
315+
fXDVF = std::make_unique<Interpolator2D>(gridLogXbj.size(),&gridLogXbj[0],gridLogQ2.size(),&gridLogQ2[0],&knotsXDVF[0]);
316+
fXDEF = std::make_unique<Interpolator2D>(gridLogXbj.size(),&gridLogXbj[0],gridLogQ2.size(),&gridLogQ2[0],&knotsXDEF[0]);
317+
fXUDF = std::make_unique<Interpolator2D>(gridLogXbj.size(),&gridLogXbj[0],gridLogQ2.size(),&gridLogQ2[0],&knotsXUDF[0]);
318+
fXSF = std::make_unique<Interpolator2D>(gridLogXbj.size(),&gridLogXbj[0],gridLogQ2.size(),&gridLogQ2[0],&knotsXSF [0]);
319+
fXGF = std::make_unique<Interpolator2D>(gridLogXbj.size(),&gridLogXbj[0],gridLogQ2.size(),&gridLogQ2[0],&knotsXGF [0]);
339320

340321
fInitialized = true;
341322
}

src/Physics/PartonDistributions/GRV98LO.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
#include "Physics/PartonDistributions/PDFModelI.h"
3333
#include "Framework/Numerical/Interpolator2D.h"
3434

35+
#include <memory>
36+
3537
namespace genie {
3638

3739
class GRV98LO: public PDFModelI {
@@ -84,12 +86,12 @@ class GRV98LO: public PDFModelI {
8486
//
8587
// arrays for the interpolation routine
8688
//
87-
Interpolator2D * fXUVF; // = f(logx,logQ2)
88-
Interpolator2D * fXDVF;
89-
Interpolator2D * fXDEF;
90-
Interpolator2D * fXUDF;
91-
Interpolator2D * fXSF;
92-
Interpolator2D * fXGF;
89+
std::unique_ptr<Interpolator2D> fXUVF; // = f(logx,logQ2)
90+
std::unique_ptr<Interpolator2D> fXDVF;
91+
std::unique_ptr<Interpolator2D> fXDEF;
92+
std::unique_ptr<Interpolator2D> fXUDF;
93+
std::unique_ptr<Interpolator2D> fXSF;
94+
std::unique_ptr<Interpolator2D> fXGF;
9395
};
9496

9597
} // genie namespace

0 commit comments

Comments
 (0)