From 5fa73c1e2e0607c16a8e088c38f5e691819b6755 Mon Sep 17 00:00:00 2001 From: Jiri Pittner Date: Mon, 26 Oct 2026 13:45:20 +0100 Subject: [PATCH] diis: fix memory leak and make reset() --- diis.h | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/diis.h b/diis.h index 2ee8c15..fd845c5 100644 --- a/diis.h +++ b/diis.h @@ -52,7 +52,8 @@ class DIIS public: DIIS() {dim=0; st=NULL; stor=NULL; errst=NULL; errstor=NULL;}; //for array of diis DIIS(const int n, const bool core=1); - void setup(const int n, const bool core=1); + void setup(const int n, const bool core=1); //can be called repeatedly, previous storage is released + void reset(); //forget the stored vectors, keep the storage ~DIIS(); typename LA_traits::normtype extrapolate(T &vec, U &errvec, bool verbose=false, const Un diiseps=0, bool errvecout=false); //vec is input/output, errvec optionally too; returns square residual norm }; @@ -64,13 +65,16 @@ st=incore?NULL: new AuxStorage; errst=incore?NULL: new AuxStorage; stor= incore? new T[dim] : NULL; errstor= incore? new U[dim] : NULL; -bmat= (Ue)0; for(int i=1; i<=n; ++i) bmat(0,i) = (Ue)-1; -aktdim=cyclicshift=0; +reset(); } template void DIIS::setup(const int n, const bool core) { +if(st) delete st; +if(errst) delete errst; +if(stor) delete[] stor; +if(errstor) delete[] errstor; dim=n; incore=core; bmat.resize(n+1); @@ -78,7 +82,13 @@ st=incore?NULL: new AuxStorage; errst=incore?NULL: new AuxStorage; stor= incore? new T[dim] : NULL; errstor= incore? new U[dim] : NULL; -bmat= (Ue)0; for(int i=1; i<=n; ++i) bmat(0,i) = (Ue)-1; +reset(); +} + +template +void DIIS::reset() +{ +bmat= (Ue)0; for(int i=1; i<=dim; ++i) bmat(0,i) = (Ue)-1; aktdim=cyclicshift=0; }