From 00a16f35c2668ceca81dd43308ebcf84b5207d82 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= <sebastien@dynare.org>
Date: Fri, 31 Mar 2023 18:36:42 +0200
Subject: [PATCH] Bytecode: cleanup memory management for UMPACK memory buffers

There was probably no bug but it is safer this way.
---
 mex/sources/bytecode/SparseMatrix.cc | 18 ++++++++++++++----
 mex/sources/bytecode/SparseMatrix.hh |  2 +-
 2 files changed, 15 insertions(+), 5 deletions(-)

diff --git a/mex/sources/bytecode/SparseMatrix.cc b/mex/sources/bytecode/SparseMatrix.cc
index b2bc13e28f..bb4bd78051 100644
--- a/mex/sources/bytecode/SparseMatrix.cc
+++ b/mex/sources/bytecode/SparseMatrix.cc
@@ -2313,9 +2313,15 @@ void
 dynSparseMatrix::End_Matlab_LU_UMFPack()
 {
   if (Symbolic)
-    umfpack_dl_free_symbolic(&Symbolic);
+    {
+      umfpack_dl_free_symbolic(&Symbolic);
+      Symbolic = nullptr;
+    }
   if (Numeric)
-    umfpack_dl_free_numeric(&Numeric);
+    {
+      umfpack_dl_free_numeric(&Numeric);
+      Numeric = nullptr;
+    }
 }
 
 void
@@ -2364,6 +2370,8 @@ dynSparseMatrix::Solve_LU_UMFPack(SuiteSparse_long *Ap, SuiteSparse_long *Ai, do
   SuiteSparse_long status = 0;
   if (iter == 0)
     {
+      if (Symbolic)
+        umfpack_dl_free_symbolic(&Symbolic);
       status = umfpack_dl_symbolic(n, n, Ap, Ai, Ax, &Symbolic, Control, Info);
       if (status < 0)
         {
@@ -2372,7 +2380,7 @@ dynSparseMatrix::Solve_LU_UMFPack(SuiteSparse_long *Ap, SuiteSparse_long *Ai, do
           throw FatalException{"umfpack_dl_symbolic failed"};
         }
     }
-  if (iter > 0)
+  if (Numeric)
     umfpack_dl_free_numeric(&Numeric);
   status = umfpack_dl_numeric(Ap, Ai, Ax, Symbolic, &Numeric, Control, Info);
   if (status < 0)
@@ -2471,6 +2479,8 @@ dynSparseMatrix::Solve_LU_UMFPack(SuiteSparse_long *Ap, SuiteSparse_long *Ai, do
   SuiteSparse_long status = 0;
   if (iter == 0)
     {
+      if (Symbolic)
+        umfpack_dl_free_symbolic(&Symbolic);
       status = umfpack_dl_symbolic(n, n, Ap, Ai, Ax, &Symbolic, Control, Info);
       if (status < 0)
         {
@@ -2479,7 +2489,7 @@ dynSparseMatrix::Solve_LU_UMFPack(SuiteSparse_long *Ap, SuiteSparse_long *Ai, do
           throw FatalException{"umfpack_dl_symbolic failed"};
         }
     }
-  if (iter > 0)
+  if (Numeric)
     umfpack_dl_free_numeric(&Numeric);
   status = umfpack_dl_numeric(Ap, Ai, Ax, Symbolic, &Numeric, Control, Info);
   if (status < 0)
diff --git a/mex/sources/bytecode/SparseMatrix.hh b/mex/sources/bytecode/SparseMatrix.hh
index 9ecc48f00f..12c5c76d14 100644
--- a/mex/sources/bytecode/SparseMatrix.hh
+++ b/mex/sources/bytecode/SparseMatrix.hh
@@ -123,7 +123,7 @@ private:
   void Delete_u(int pos);
   void Clear_u();
   void Print_u() const;
-  void *Symbolic, *Numeric;
+  void *Symbolic {nullptr}, *Numeric {nullptr};
   void Check_the_Solution(int periods, int y_kmin, int y_kmax, int Size, double *u, int *pivot, int *b);
   int complete(int beg_t, int Size, int periods, int *b);
   void bksub(int tbreak, int last_period, int Size, double slowc_l);
-- 
GitLab