diff --git a/source/source_lcao/module_operator_lcao/dspin_lcao.cpp b/source/source_lcao/module_operator_lcao/dspin_lcao.cpp index 6d487b2fe2..8f8e975587 100644 --- a/source/source_lcao/module_operator_lcao/dspin_lcao.cpp +++ b/source/source_lcao/module_operator_lcao/dspin_lcao.cpp @@ -228,8 +228,21 @@ void hamilt::DeltaSpin>::cal_pre_HR() } this->paraV = this->hR->get_paraV(); ModuleBase::timer::start("DeltaSpin", "cal_pre_HR"); + // Reset the caches from a possible previous initialization: + // pre_hr holds raw HContainer pointers, so clear() alone would leak them, + // and stale B_I_data would survive for atoms that are no longer constrained. + for (auto& hr : this->pre_hr) + { + delete hr; + } this->pre_hr.clear(); this->pre_hr.resize(this->ucell->nat, nullptr); + for (auto& b : this->B_I_data) + { + b.clear(); + } + this->B_I_data.clear(); + this->B_I_data.resize(this->ucell->nat); const int npol = this->ucell->get_npol(); size_t memory_cost = 0;