From a46ba8d3db5b80f4c6d312d1ce5d268967cb8767 Mon Sep 17 00:00:00 2001
From: Fons Rademakers <Fons.Rademakers@cern.ch>
Date: Tue, 9 Apr 2002 13:42:15 +0000
Subject: [PATCH] fix memory leak in case max_value < singularity_tolerance.
 Found by Paul Balm.

git-svn-id: http://root.cern.ch/svn/root/trunk@4332 27541ba8-7e3a-0410-8455-c3a389f83636
---
 matrix/src/TMatrix.cxx  | 10 ++++++++--
 matrix/src/TMatrixD.cxx | 13 +++++++++----
 2 files changed, 17 insertions(+), 6 deletions(-)

diff --git a/matrix/src/TMatrix.cxx b/matrix/src/TMatrix.cxx
index a74a700f34e..d35c7878d01 100644
--- a/matrix/src/TMatrix.cxx
+++ b/matrix/src/TMatrix.cxx
@@ -1,4 +1,4 @@
-// @(#)root/matrix:$Name:  $:$Id: TMatrix.cxx,v 1.13 2002/01/04 08:14:30 brun Exp $
+// @(#)root/matrix:$Name:  $:$Id: TMatrix.cxx,v 1.14 2002/02/27 08:39:26 brun Exp $
 // Author: Fons Rademakers   03/11/97
 
 /*************************************************************************
@@ -979,7 +979,11 @@ TMatrix &TMatrix::Invert(Double_t *determ_ptr)
                   if (!was_pivoted[k] && (curr_value = TMath::Abs(*cp)) > max_value)
                      max_value = curr_value, prow = k, pcol = j;
              }
-         if (max_value < singularity_tolerance)
+         if (max_value < singularity_tolerance) {
+            // free allocated heap memory before returning
+            if (symmetric) delete [] diag;
+            delete [] pivots;
+            delete [] was_pivoted;
             if (determ_ptr) {
                *determ_ptr = 0;
                return *this;
@@ -987,6 +991,7 @@ TMatrix &TMatrix::Invert(Double_t *determ_ptr)
                Error("Invert(Double_t*)", "matrix turns out to be singular: can't invert");
                return *this;
             }
+         }
          pivotp->row = prow;
          pivotp->col = pcol;
      }
@@ -1054,6 +1059,7 @@ TMatrix &TMatrix::Invert(Double_t *determ_ptr)
 
    delete [] was_pivoted;
    delete [] pivots;
+
    return *this;
 }
 
diff --git a/matrix/src/TMatrixD.cxx b/matrix/src/TMatrixD.cxx
index efdfe8150b3..62798bd4e80 100644
--- a/matrix/src/TMatrixD.cxx
+++ b/matrix/src/TMatrixD.cxx
@@ -1,4 +1,4 @@
-// @(#)root/matrix:$Name:  $:$Id: TMatrixD.cxx,v 1.14 2002/01/30 07:00:30 brun Exp $
+// @(#)root/matrix:$Name:  $:$Id: TMatrixD.cxx,v 1.15 2002/02/27 08:39:27 brun Exp $
 // Author: Fons Rademakers   03/11/97
 
 /*************************************************************************
@@ -981,7 +981,11 @@ TMatrixD &TMatrixD::Invert(Double_t *determ_ptr)
                   if (!was_pivoted[k] && (curr_value = TMath::Abs(*cp)) > max_value)
                      max_value = curr_value, prow = k, pcol = j;
              }
-         if (max_value < singularity_tolerance)
+         if (max_value < singularity_tolerance) {
+            // free allocated heap memory before returning
+            if (symmetric) delete [] diag;
+            delete [] pivots;
+            delete [] was_pivoted;
             if (determ_ptr) {
                *determ_ptr = 0;
                return *this;
@@ -989,6 +993,7 @@ TMatrixD &TMatrixD::Invert(Double_t *determ_ptr)
                Error("Invert(Double_t*)", "matrix turns out to be singular: can't invert");
                return *this;
             }
+         }
          pivotp->row = prow;
          pivotp->col = pcol;
      }
@@ -1056,7 +1061,7 @@ TMatrixD &TMatrixD::Invert(Double_t *determ_ptr)
 
    delete [] was_pivoted;
    delete [] pivots;
-   //delete [] diag;
+
    return *this;
 }
 
@@ -1308,7 +1313,7 @@ TVectorD &e)
     a.Zero();
     d.Zero();
     e.Zero();
-    return; 
+    return;
   }
 
   Double_t *pa = a.fElements;
-- 
GitLab