From 3392d185c99b91d560088b4e301818307e52499a Mon Sep 17 00:00:00 2001
From: Giovanni La Mura <giovanni.lamura@inaf.it>
Date: Fri, 15 Mar 2024 10:15:56 +0100
Subject: [PATCH] Remove C1_AddOns::allocate_vectors() and fix vector memory
 leak

---
 src/include/Commons.h   | 13 -------------
 src/libnptm/Commons.cpp | 21 +++++----------------
 2 files changed, 5 insertions(+), 29 deletions(-)

diff --git a/src/include/Commons.h b/src/include/Commons.h
index b522ecde..3bd517d0 100644
--- a/src/include/Commons.h
+++ b/src/include/Commons.h
@@ -208,19 +208,6 @@ protected:
   //! \brief Maximum expansion order plus one. QUESTION: correct?
   int lmpo;
 
-  /*! \brief Allocate the necessary common vectors depending on configuration.
-   *
-   * The size of the vectors and matrices defined in various common
-   * blocks, and particularly in C1, depends on many settings of the
-   * problem configuration, such as the number of spheres, the number
-   * of layers the spheres are made of, the field expansion order and
-   * others. This function collects the calculations needed to infer
-   * the necessary amount of memory for these configurable elements,
-   * thus making the class constructor more compact and easier to handle.
-   *
-   * \param c4: `C4 *` Pointer to a C4 structure.
-   */
-  void allocate_vectors(C4 *c4);
 public:
   //! \brief QUESTION: definition?
   dcomplex *vh;
diff --git a/src/libnptm/Commons.cpp b/src/libnptm/Commons.cpp
index c9d5aeb2..e93af0c2 100644
--- a/src/libnptm/Commons.cpp
+++ b/src/libnptm/Commons.cpp
@@ -132,6 +132,11 @@ C1_AddOns::C1_AddOns(C4 *c4) {
   }
   vintm = new dcomplex[16]();
   vintt = new dcomplex[16]();
+  // This calculates the size of v3j0
+  const int nv3j = c4->nv3j;
+  v3j0 = new double[nv3j]();
+  ind3j = new int*[lmpo];
+  for (int ii = 0; ii < lmpo; ii++) ind3j[ii] = new int[c4->lm]();
   fsac = new dcomplex*[2];
   sac = new dcomplex*[2];
   fsacm = new dcomplex*[2];
@@ -144,7 +149,6 @@ C1_AddOns::C1_AddOns(C4 *c4) {
   ecscp = new dcomplex[2]();
   scscpm = new dcomplex[2]();
   ecscpm = new dcomplex[2]();
-  allocate_vectors(c4);
   sscs = new double[nsph]();
   ecscm = new double[2]();
   scscm = new double[2]();
@@ -185,21 +189,6 @@ C1_AddOns::~C1_AddOns() {
   delete[] ecsc;
 }
 
-void C1_AddOns::allocate_vectors(C4 *c4) {
-  // This calculates the size of v3j0
-  int lm = (c4->li > c4->le) ? c4->li : c4->le;
-  const int nv3j = c4->nv3j;
-  v3j0 = new double[nv3j]();
-  ind3j = new int*[lmpo];
-  for (int ii = 0; ii < lmpo; ii++) ind3j[ii] = new int[c4->lm]();
-  // This calculates the size of vyhj
-  int ivy = (nsph * nsph - 1) * c4->litpos;
-  vyhj = new dcomplex[ivy]();
-  // This calculates the size of vyj0
-  ivy = c4->lmtpos * c4->nsph;
-  vyj0 = new dcomplex[ivy]();
-}
-
 C2::C2(int ns, int nl, int npnt, int npntts) {
   nsph = ns;
   int max_n = (npnt > npntts) ? npnt : npntts;
-- 
GitLab