Skip to content

Commit 6fadfb9

Browse files
Refactor: decrease uninitialized member values and the risk of memory leaks in abacus (#5755)
* init variable and fix memory leak bug in module_base * fix uninit member var in module_basis * chang logic bug and uninit memeber value in the module cell * chang logic bug and uninit memeber value in the module_elecstate * chang logic bug and uninit memeber value in the module_eslover * chang memory leak risk and uninit memeber value in the module_hamilt_general * chang uninit memeber value in the module_hamilt_lcao * chang uninit memeber value in the module_hamilt_pwdft * fix in klist * [pre-commit.ci lite] apply automatic fixes * fix useless assert * revert change in read_pp_upf100.cpp * change uninit memeber value in module_hamilt_pw * change uninit memeber value in module_hsolver * change uninit memeber value in module_io * change uninit memeber value in module_md and module_relax * [pre-commit.ci lite] apply automatic fixes * update nnr init data * fix uninit member value * update value in elecstate_energy.cpp * add init parameter for construct in sto_func * add value in the construct file * [pre-commit.ci lite] apply automatic fixes * change magnetism.cpp * [pre-commit.ci lite] apply automatic fixes * modify logic of elecstate_energy.cpp --------- Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
1 parent 3abf2fc commit 6fadfb9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+427
-333
lines changed

source/module_base/intarray.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,14 @@ class IntArray
141141
}
142142

143143
private:
144-
int size;
145-
int dim;
146-
int bound1, bound2, bound3, bound4, bound5, bound6;
144+
int size=0;
145+
int dim=0;
146+
int bound1=0;
147+
int bound2=0;
148+
int bound3=0;
149+
int bound4=0;
150+
int bound5=0;
151+
int bound6=0;
147152
static int arrayCount;
148153
void freemem();
149154
};

source/module_base/inverse_matrix.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ class Inverse_Matrix_Complex
2020
void init( const int &dim_in);
2121

2222
private:
23-
int dim;
24-
double *e;
25-
int lwork;
26-
std::complex<double> *work2;
27-
double* rwork;
28-
int info;
29-
bool allocate; //mohan add 2012-04-02
23+
int dim=0;
24+
double *e=nullptr;
25+
int lwork=0;
26+
std::complex<double> *work2=nullptr;
27+
double* rwork=nullptr;
28+
int info=0;
29+
bool allocate=false; //mohan add 2012-04-02
3030

3131
ModuleBase::ComplexMatrix EA;
3232
};

source/module_base/memory.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,7 @@ void Memory::print_all(std::ofstream &ofs)
436436
#if defined(__CUDA) || defined(__ROCM)
437437
if(!init_flag_gpu)
438438
{
439+
delete[] print_flag;
439440
return;
440441
}
441442

source/module_basis/module_ao/ORB_nonlocal.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Numerical_Nonlocal::Numerical_Nonlocal()
77
//question remains
88
this->type = 0;
99
this->lmax = 0;
10+
this->rcut_max = 0.0;
1011
this->Proj = new Numerical_Nonlocal_Lm[1];
1112
this->nproj = -1;
1213
//zhengdy-soc, for optimize nonlocal part
@@ -49,6 +50,7 @@ void Numerical_Nonlocal::set_type_info
4950
//----------------------------------------------------------
5051
//only store radial function
5152
delete[] Proj;
53+
this->Proj = nullptr;
5254
this->Proj = new Numerical_Nonlocal_Lm[this->nproj];
5355

5456
for (int p1=0; p1<nproj; p1++)

source/module_basis/module_ao/ORB_nonlocal.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,20 +44,20 @@ class Numerical_Nonlocal
4444

4545
Numerical_Nonlocal_Lm* Proj; ///< length: nproj(only store radial function )
4646

47-
const double& get_rcut_max(void) const { return rcut_max; }
47+
const double& get_rcut_max() const { return rcut_max; }
4848
const int& get_nproj() const { return nproj; }
4949

5050
private:
5151

52-
std::string label; /// <element label
52+
std::string label=""; /// <element label
5353

5454
int type; ///< element index
5555

5656
int lmax; ///< max value of L angular momentum
5757

58-
double rcut_max;
58+
double rcut_max=0.0;
5959

60-
std::string type_ps; ///<local or nonlocal
60+
std::string type_ps=""; ///<local or nonlocal
6161

6262
int nproj;
6363

source/module_basis/module_ao/ORB_nonlocal_lm.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Numerical_Nonlocal_Lm::Numerical_Nonlocal_Lm()
1616
label = "";
1717
index_atom_type = 0;
1818
angular_momentum_l = 0;
19+
index_proj = 0;
1920

2021
nr = 1;
2122
nk = 1;
@@ -34,7 +35,7 @@ Numerical_Nonlocal_Lm::~Numerical_Nonlocal_Lm()
3435
this->freemem();
3536
}
3637

37-
void Numerical_Nonlocal_Lm::renew(void)
38+
void Numerical_Nonlocal_Lm::renew()
3839
{
3940
assert(nr_uniform>0);
4041
assert(nr>0);
@@ -55,7 +56,7 @@ void Numerical_Nonlocal_Lm::renew(void)
5556
ModuleBase::GlobalFunc::ZEROS(beta_k, nk);
5657
}
5758

58-
void Numerical_Nonlocal_Lm::freemem(void)
59+
void Numerical_Nonlocal_Lm::freemem()
5960
{
6061
delete[] this->r_radial;
6162
delete[] this->rab;
@@ -246,7 +247,7 @@ void Numerical_Nonlocal_Lm::extra_uniform(const double &dr_uniform_in)
246247
}
247248
*/
248249

249-
void Numerical_Nonlocal_Lm::get_kradial(void)
250+
void Numerical_Nonlocal_Lm::get_kradial()
250251
{
251252
//ModuleBase::TITLE("Numerical_Nonlocal_Lm","get_kradial");
252253
double *jl = new double[nr];

source/module_basis/module_ao/parallel_orbitals.cpp

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,20 @@
66

77
Parallel_Orbitals::Parallel_Orbitals()
88
{
9-
loc_sizes = nullptr;
10-
9+
this->loc_sizes = nullptr;
1110
// in multi-k, 2D-block-division variables for FT (R<->k)
12-
nnr = 1;
13-
nlocdim = nullptr;
14-
nlocstart = nullptr;
11+
this->nlocdim = nullptr;
12+
this->nlocstart = nullptr;
13+
this->nnr = 1;
14+
this->ncol_bands = 0;
15+
this->nrow_bands=0;
16+
this->nloc_wfc=0;
17+
this->nloc_Eij=0;
18+
this->lastband_in_proc=0;
19+
this->lastband_number=0;
20+
this->loc_size=0;
21+
this->nbands = 0;
22+
1523
}
1624

1725
Parallel_Orbitals::~Parallel_Orbitals()

source/module_basis/module_ao/parallel_orbitals.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class Parallel_Orbitals : public Parallel_2D
2929
/// number of elements(basis-pairs) in this processon
3030
/// on all adjacent atoms-pairs(2D division)
3131
///---------------------------------------
32-
int nnr;
32+
int nnr=1;
3333
int *nlocdim;
3434
int *nlocstart;
3535

@@ -80,7 +80,7 @@ class Parallel_Orbitals : public Parallel_2D
8080

8181
int get_nbands() const;
8282

83-
int nbands = 0;
83+
int nbands;
8484

8585
/**
8686
* @brief gather global indexes of orbitals in this processor

source/module_basis/module_pw/pw_basis_big.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,12 @@ class PW_Basis_Big : public PW_Basis_Sup
3434
bxyz = bx * by * bz;
3535
}
3636
int bx = 1, by = 1, bz = 1, bxyz = 1;
37-
int nbx, nby, nbz;
38-
int nbzp;
39-
int nbxx;
40-
int nbzp_start;
37+
int nbx=0;
38+
int nby=0;
39+
int nbz=0;
40+
int nbzp=0;
41+
int nbxx=0;
42+
int nbzp_start=0;
4143

4244
void autoset_big_cell_size(int& b_size, const int& nc_size, const int nproc = 0)
4345
{

source/module_basis/module_pw/pw_basis_k.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,9 @@ class PW_Basis_K : public PW_Basis
9494
double *gk2=nullptr; // modulus (G+K)^2 of G vectors [npwk_max*nks]
9595

9696
// liuyu add 2023-09-06
97-
double erf_ecut; // the value of the constant energy cutoff
98-
double erf_height; // the height of the energy step for reciprocal vectors
99-
double erf_sigma; // the width of the energy step for reciprocal vectors
97+
double erf_ecut=0.0; // the value of the constant energy cutoff
98+
double erf_height=0.0; // the height of the energy step for reciprocal vectors
99+
double erf_sigma=0.0; // the width of the energy step for reciprocal vectors
100100

101101
//collect gdirect, gcar, gg
102102
void collect_local_pw(const double& erf_ecut_in = 0.0,

0 commit comments

Comments
 (0)