Skip to content

Commit 12fff4c

Browse files
author
abacus_fixer
committed
fix(esolver): manage dftu_ via std::unique_ptr to prevent memory leak
ESolver_KS::dftu_ was allocated with new in both ESolver_KS_PW and ESolver_KS_LCAO constructors but never deleted in ~ESolver_KS, contradicting the member comment and commit 156a5d8. Convert dftu_ to std::unique_ptr<Plus_U_Base> so ownership is explicit and the object is released automatically on destruction. Call sites that pass raw pointers to downstream functions use .get(); C++11 baseline requires reset(new ...) instead of std::make_unique.
1 parent 41493ee commit 12fff4c

6 files changed

Lines changed: 12 additions & 12 deletions

File tree

source/source_esolver/esolver_dfpt_pw.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ void ESolver_DFPT_PW::init_dfpt(UnitCell& ucell)
354354

355355
dfpt_->init(ucell, *this->stp.psi_cpu, this->pw_rho, this->pw_wfc, &this->sf, veff_r,
356356
this->pelec->wg, this->pelec->ekb, xc_adapter_, nelec_, ecutwfc_,
357-
dft_plus_u_ ? this->dftu_ : nullptr);
357+
dft_plus_u_ ? this->dftu_.get() : nullptr);
358358
}
359359

360360
void ESolver_DFPT_PW::run_post_process(UnitCell& ucell)

source/source_esolver/esolver_double_xc.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ void ESolver_DoubleXC<TK, TR>::before_scf(UnitCell& ucell, const int istep)
148148
this->two_center_bundle_,
149149
this->orb_,
150150
this->dmat_base.dm,
151-
this->dftu_,
151+
this->dftu_.get(),
152152
this->deepks,
153153
istep,
154154
this->exx_nao,

source/source_esolver/esolver_ks.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ class ESolver_KS : public ESolver_FP
6060
//! nonlocal pseudopotentials
6161
pseudopot_cell_vnl ppcell;
6262

63-
//! DFT+U method (base-class pointer; PW news Plus_U_Base, LCAO news Plus_U),
64-
//! allocated in the derived-class constructor, deleted in ~ESolver_KS.
65-
Plus_U_Base* dftu_ = nullptr;
63+
//! DFT+U method (base-class pointer; PW news Plus_U_Base, LCAO news Plus_U).
64+
//! Owned by ESolver_KS via unique_ptr; released automatically in ~ESolver_KS.
65+
std::unique_ptr<Plus_U_Base> dftu_;
6666

6767
std::string basisname; //! esolver_ks_lcao.cpp
6868
double esolver_KS_ne = 0.0; //! number of electrons

source/source_esolver/esolver_ks_lcao.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ ESolver_KS_LCAO<TK, TR>::ESolver_KS_LCAO()
3636
{
3737
this->classname = "ESolver_KS_LCAO";
3838
this->basisname = "LCAO";
39-
this->dftu_ = new Plus_U();
39+
this->dftu_.reset(new Plus_U());
4040
}
4141

4242
template <typename TK, typename TR>
@@ -158,7 +158,7 @@ void ESolver_KS_LCAO<TK, TR>::before_scf(UnitCell& ucell, const int istep)
158158
{
159159
this->p_hamilt = new hamilt::HamiltLCAO<TK, TR>(
160160
ucell, this->gd, &this->pv, this->pelec->pot, this->kv,
161-
two_center_bundle_, orb_, this->dmat.dm, this->dftu_, this->deepks, istep, exx_nao, this->exx_info_);
161+
two_center_bundle_, orb_, this->dmat.dm, this->dftu_.get(), this->deepks, istep, exx_nao, this->exx_info_);
162162
}
163163

164164
// 9) for each ionic step, the overlap <phi|alpha> must be rebuilt
@@ -510,7 +510,7 @@ void ESolver_KS_LCAO<TK, TR>::iter_finish(UnitCell& ucell, const int istep, int&
510510
const std::vector<std::vector<TK>>& dm_vec = this->dmat.dm->get_DMK_vector();
511511

512512
// 1) calculate the local occupation number matrix and energy correction in DFT+U
513-
finish_dftu_lcao<TK>(iter, conv_esolver, this->inp_->dft_plus_u, this->inp_->out_chg[0], this->dftu_, ucell, dm_vec, this->kv, this->p_chgmix->get_mixing_beta(), hamilt_lcao, PARAM.globalv.global_out_dir, this->inp_->nspin, PARAM.globalv.npol, PARAM.globalv.gamma_only_local);
513+
finish_dftu_lcao<TK>(iter, conv_esolver, this->inp_->dft_plus_u, this->inp_->out_chg[0], this->dftu_.get(), ucell, dm_vec, this->kv, this->p_chgmix->get_mixing_beta(), hamilt_lcao, PARAM.globalv.global_out_dir, this->inp_->nspin, PARAM.globalv.npol, PARAM.globalv.gamma_only_local);
514514

515515
// mohan add 2025-11: push DFT+U energy from Plus_U instance to ElecState.
516516
// Covers both dft_plus_u==1 (new method, energy accumulated by DFTU::contributeHR

source/source_esolver/esolver_ks_pw.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ ESolver_KS_PW<T, Device>::ESolver_KS_PW()
3333
this->classname = "ESolver_KS_PW";
3434
this->basisname = "PW";
3535
// PW basis: the DFT+U object is the base class (no LCAO orbitals).
36-
this->dftu_ = new Plus_U_Base();
36+
this->dftu_.reset(new Plus_U_Base());
3737
}
3838

3939
template <typename T, typename Device>
@@ -67,7 +67,7 @@ void ESolver_KS_PW<T, Device>::allocate_hamilt(const UnitCell& ucell)
6767
this->pw_wfc,
6868
&this->kv,
6969
&this->ppcell,
70-
this->dftu_,
70+
this->dftu_.get(),
7171
&ucell,
7272
&this->general_exx_info_);
7373
}
@@ -385,7 +385,7 @@ void ESolver_KS_PW<T, Device>::cal_force(BaseCell& basecell, ModuleBase::matrix&
385385
&ucell.symm,
386386
&this->sf,
387387
this->solvent,
388-
this->dftu_,
388+
this->dftu_.get(),
389389
&this->locpp,
390390
&this->ppcell,
391391
&this->kv,

source/source_esolver/lcao_others.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ void ESolver_KS_LCAO<TK, TR>::others(BaseCell& basecell, const int istep)
133133
two_center_bundle_,
134134
orb_,
135135
this->dmat.dm,
136-
this->dftu_,
136+
this->dftu_.get(),
137137
this->deepks,
138138
istep,
139139
this->exx_nao,

0 commit comments

Comments
 (0)