Skip to content

Commit aa8358c

Browse files
authored
Fix: memory leak in sltk_grid.cpp (#2141)
1 parent b3c7428 commit aa8358c

File tree

2 files changed

+23
-18
lines changed

2 files changed

+23
-18
lines changed

source/module_cell/module_neighbor/sltk_grid.cpp

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -57,24 +57,7 @@ Grid::Grid(const int &test_grid_in):test_grid(test_grid_in)
5757
Grid::~Grid()
5858
{
5959
delete[] atomlink;
60-
if (this->init_cell_flag)
61-
{
62-
for (int i = 0;i < this->dx;i++)
63-
{
64-
for (int j = 0;j < this->dy;j++)
65-
{
66-
delete[] this->Cell[i][j];
67-
}
68-
}
69-
70-
for (int i = 0;i < this->dx;i++)
71-
{
72-
delete[] this->Cell[i];
73-
}
74-
75-
delete[] this->Cell;
76-
this->init_cell_flag = false;
77-
}
60+
this->delete_Cell();
7861

7962
}
8063

@@ -102,6 +85,7 @@ void Grid::setMemberVariables(
10285
{
10386
ModuleBase::TITLE("SLTK_Grid", "setMemberVariables");
10487

88+
this->delete_Cell();
10589
// mohan add 2010-09-05
10690
AdjacentSet::call_times = 0;
10791

source/module_cell/module_neighbor/sltk_grid.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,27 @@ class Grid
8888
double cell_y_length;
8989
double cell_z_length;
9090
CellSet ***Cell; //dx , dy ,dz is cell number in each direction,respectly.
91+
void delete_Cell() //it will replace by container soon!
92+
{
93+
if (this->init_cell_flag)
94+
{
95+
for (int i = 0;i < this->dx;i++)
96+
{
97+
for (int j = 0;j < this->dy;j++)
98+
{
99+
delete[] this->Cell[i][j];
100+
}
101+
}
102+
103+
for (int i = 0;i < this->dx;i++)
104+
{
105+
delete[] this->Cell[i];
106+
}
107+
108+
delete[] this->Cell;
109+
this->init_cell_flag = false;
110+
}
111+
}
91112

92113
double grid_length[3];
93114
double vec1[3];

0 commit comments

Comments
 (0)