Skip to content

Commit 1ed97ae

Browse files
committed
fix: memory leak
1. void getfftixy2is(int*), and add unit test for this function 2. forget to delete pw_wfc in ESolver_KS
1 parent d295d5d commit 1ed97ae

File tree

7 files changed

+19
-15
lines changed

7 files changed

+19
-15
lines changed

source/module_esolver/esolver.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ namespace ModuleESolver
4545

4646
void clean_esolver(ESolver*& pesolver)
4747
{
48-
if (pesolver != NULL)
48+
if (pesolver != nullptr)
4949
{
5050
delete pesolver;
5151
}

source/module_esolver/esolver_fp.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ namespace ModuleESolver
1414
}
1515
ESolver_FP::~ESolver_FP()
1616
{
17-
if(pw_rho!=NULL) delete pw_rho;
17+
if(pw_rho!=nullptr) delete pw_rho;
1818
}
1919
void ESolver_FP::Init(Input& inp, UnitCell_pseudo& cell)
2020
{

source/module_esolver/esolver_ks.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,14 @@ namespace ModuleESolver
3636
tmp->setbxyz(INPUT.bx,INPUT.by,INPUT.bz);
3737
}
3838

39+
ESolver_KS::~ESolver_KS()
40+
{
41+
if(this->pw_wfc != nullptr) delete this->pw_wfc;
42+
if(this->pelec != nullptr) delete this->pelec;
43+
if(this->phami != nullptr) delete this->phami;
44+
if(this->phsol != nullptr) delete this->phsol;
45+
}
46+
3947
void ESolver_KS::Init(Input& inp, UnitCell_pseudo& ucell)
4048
{
4149
ESolver_FP::Init(inp,ucell);

source/module_esolver/esolver_ks.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ namespace ModuleESolver
1616
{
1717
public:
1818
ESolver_KS();
19+
virtual ~ESolver_KS();
1920
// HSolver* phsol;
2021
double diag_ethr; // diag threshold
2122
double scf_thr; // scf threshold

source/module_esolver/esolver_ks_pw.cpp

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,18 +48,6 @@ namespace ModuleESolver
4848
}
4949
ESolver_KS_PW::~ESolver_KS_PW()
5050
{
51-
if(this->pelec!=nullptr)
52-
{
53-
delete this->pelec;
54-
}
55-
if(this->phami!=nullptr)
56-
{
57-
delete this->phami;
58-
}
59-
if(this->phsol!=nullptr)
60-
{
61-
delete this->phsol;
62-
}
6351
}
6452

6553
void ESolver_KS_PW::Init(Input& inp, UnitCell_pseudo& ucell)

source/module_pw/pw_basis.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ void PW_Basis::getfftixy2is(int * fftixy2is)
191191
//Note: please assert when is1 >= is2, fftixy2is[is1] >= fftixy2is[is2]!
192192
for(int ixy = 0 ; ixy < this->fftnxy ; ++ixy) fftixy2is[ixy] = -1;
193193
int ixy = 0;
194-
for(int is = 0; is < this->nst+1; ++is)
194+
for(int is = 0; is < this->nst; ++is)
195195
{
196196
for(; ixy < this->fftnxy ; ++ixy)
197197
{

source/module_pw/test/test1-1-1.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,13 @@ TEST_F(PWTEST,test1_1_1)
216216
{
217217
EXPECT_NEAR(pwtest.gg[ig], pwtest.gg_uniq[pwtest.ig2igg[ig]],1e-8);
218218
}
219+
int * irindex = new int [pwtest.fftnxy];
220+
pwtest.getfftixy2is(irindex);
221+
for(int is = 0 ; is < pwtest.nst ;++is)
222+
{
223+
EXPECT_EQ(irindex[pwtest.is2fftixy[is]],is);
224+
}
225+
delete[] irindex;
219226

220227

221228
}

0 commit comments

Comments
 (0)