diff --git a/matrix/src/TMatrix.cxx b/matrix/src/TMatrix.cxx
index a74a700f34e53ba2efad2a77cd1b8ca325bd1239..d35c7878d01a8f73e4685cee657911815272af0f 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 efdfe8150b3c51997b0ab7a80f58db4ba10695bc..62798bd4e8005f6939843f90d58baaeda98fa5bc 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;