From dfec145e66b76736b182a8b6a2338ae39b8f287e Mon Sep 17 00:00:00 2001
From: Giovanni La Mura <giovanni.lamura@inaf.it>
Date: Mon, 6 Nov 2023 11:43:31 +0100
Subject: [PATCH] Clean memory after execution of dme()

---
 src/include/sph_subs.h | 22 +++++++++++++++++-----
 1 file changed, 17 insertions(+), 5 deletions(-)

diff --git a/src/include/sph_subs.h b/src/include/sph_subs.h
index 50ea288d..3c562fb1 100644
--- a/src/include/sph_subs.h
+++ b/src/include/sph_subs.h
@@ -1,6 +1,6 @@
 /*! \file sph_subs.h
  *
- * \brief C++ porting of SPH functions, subroutines and data structures.
+ * \brief C++ porting of SPH functions and subroutines.
  *
  * Remember that FORTRAN passes arguments by reference, so, every time we use
  * a subroutine call, we need to add a referencing layer to the C++ variable.
@@ -11,11 +11,14 @@
  * types are given, to avoid doxygen warning messages.
  */
 
-#ifndef SRC_INCLUDE_SPH_SUBS_H_
-#define SRC_INCLUDE_SPH_SUBS_H_
+#ifndef INCLUDE_COMMONS_H_
+#include "Commons.h"
+#endif
+
+#ifndef INCLUDE_SPH_SUBS_H_
+#define INCLUDE_SPH_SUBS_H_
 
 #include <complex>
-#include "Commons.h"
 
 /*! \brief Conjugate of a double precision complex number
  *
@@ -1346,7 +1349,6 @@ void wmasp(
 void dme(
 		int li, int i, int npnt, int npntts, double vk, double exdc, double exri,
 		C1 *c1, C2 *c2, int &jer, int &lcalc, std::complex<double> &arg) {
-	//double rfj[42], rfn[42];
 	double *rfj = new double[42];
 	double *rfn = new double[42];
 	std::complex<double> cfj[42], fbi[42], fb[42], fn[42];
@@ -1371,6 +1373,8 @@ void dme(
 		cbf(lipo, arg, lcalc, cfj);
 		if (lcalc < lipo) {
 			jer = 5;
+			delete[] rfj;
+			delete[] rfn;
 			return;
 		}
 		for (int j24 = 1; j24 <= lipt; j24++) fbi[j24 - 1] = cfj[j24 - 1];
@@ -1380,6 +1384,8 @@ void dme(
 		rbf(lipo, arg.real(), lcalc, rfj);
 		if (lcalc < lipo) {
 			jer = 5;
+			delete[] rfj;
+			delete[] rfn;
 			return;
 		}
 		for (int j30 = 1; j30 <= lipt; j30++) fbi[j30 - 1] = rfj[j30 - 1];
@@ -1389,11 +1395,15 @@ void dme(
 	rbf(lipo, arex, lcalc, rfj);
 	if (lcalc < lipo) {
 		jer = 7;
+		delete[] rfj;
+		delete[] rfn;
 		return;
 	}
 	rnf(lipo, arex, lcalc, rfn);
 	if (lcalc < lipo) {
 		jer = 8;
+		delete[] rfj;
+		delete[] rfn;
 		return;
 	}
 	for (int j43 = 1; j43 <= lipt; j43++) {
@@ -1473,6 +1483,8 @@ void dme(
 			//printf("DEBUG: gone 90, rei[%d][%d] = (%lE,%lE)\n", l90, i, c1->rei[l90 - 1][i - 1].real(), c1->rei[l90 - 1][i - 1].imag());
 		}
 	} // nsh <= 1 ?
+	delete[] rfj;
+	delete[] rfn;
 	return;
 }
 
-- 
GitLab