From 610a87c0a2d2339cbeea08dd8fc0daf29650ee18 Mon Sep 17 00:00:00 2001
From: Lorenzo Moneta <Lorenzo.Moneta@cern.ch>
Date: Mon, 3 Jun 2019 11:38:30 +0200
Subject: [PATCH] fix memory leak when fitting histograms using linear fitter
 and functions built from expression using special operator ++. This fixes
 ROOT-10147

---
 hist/hist/src/TGraph.cxx      | 17 ++++++++---------
 hist/hist/src/TGraph2D.cxx    | 18 +++++++++---------
 hist/hist/src/TH1.cxx         | 12 ++++++------
 hist/hist/src/TMultiGraph.cxx | 13 ++++++-------
 4 files changed, 29 insertions(+), 31 deletions(-)

diff --git a/hist/hist/src/TGraph.cxx b/hist/hist/src/TGraph.cxx
index 935765df2b9..69b80e49eda 100644
--- a/hist/hist/src/TGraph.cxx
+++ b/hist/hist/src/TGraph.cxx
@@ -1052,15 +1052,14 @@ TFitResultPtr TGraph::Fit(const char *fname, Option_t *option, Option_t *, Axis_
 {
    char *linear;
    linear = (char*) strstr(fname, "++");
-   TF1 *f1 = 0;
-   if (linear)
-      f1 = new TF1(fname, fname, xmin, xmax);
-   else {
-      f1 = (TF1*)gROOT->GetFunction(fname);
-      if (!f1) {
-         Printf("Unknown function: %s", fname);
-         return -1;
-      }
+   if (linear) { 
+      TF1 f1(fname, fname, xmin, xmax);
+      return Fit(&f1, option, "", xmin, xmax);
+   }
+   TF1 * f1 = (TF1*)gROOT->GetFunction(fname);
+   if (!f1) {
+      Printf("Unknown function: %s", fname);
+      return -1;
    }
    return Fit(f1, option, "", xmin, xmax);
 }
diff --git a/hist/hist/src/TGraph2D.cxx b/hist/hist/src/TGraph2D.cxx
index 40f73c27529..6fc44b57c81 100644
--- a/hist/hist/src/TGraph2D.cxx
+++ b/hist/hist/src/TGraph2D.cxx
@@ -760,15 +760,15 @@ TFitResultPtr TGraph2D::Fit(const char *fname, Option_t *option, Option_t *)
 
    char *linear;
    linear = (char*)strstr(fname, "++");
-   TF2 *f2 = 0;
-   if (linear)
-      f2 = new TF2(fname, fname);
-   else {
-      f2 = (TF2*)gROOT->GetFunction(fname);
-      if (!f2) {
-         Printf("Unknown function: %s", fname);
-         return -1;
-      }
+
+   if (linear) { 
+      TF2 f2(fname, fname);
+      return Fit(&f2, option, "");
+   }
+   TF2 * f2 = (TF2*)gROOT->GetFunction(fname);
+   if (!f2) {
+      Printf("Unknown function: %s", fname);
+      return -1;
    }
    return Fit(f2, option, "");
 
diff --git a/hist/hist/src/TH1.cxx b/hist/hist/src/TH1.cxx
index 776b1764527..81bb3df3ea9 100644
--- a/hist/hist/src/TH1.cxx
+++ b/hist/hist/src/TH1.cxx
@@ -3800,16 +3800,16 @@ TFitResultPtr TH1::Fit(const char *fname ,Option_t *option ,Option_t *goption, D
    Int_t ndim=GetDimension();
    if (linear){
       if (ndim<2){
-         f1=new TF1(fname, fname, xxmin, xxmax);
-         return Fit(f1,option,goption,xxmin,xxmax);
+         TF1 f1(fname, fname, xxmin, xxmax);
+         return Fit(&f1,option,goption,xxmin,xxmax);
       }
       else if (ndim<3){
-         f2=new TF2(fname, fname);
-         return Fit(f2,option,goption,xxmin,xxmax);
+         TF2 f2(fname, fname);
+         return Fit(&f2,option,goption,xxmin,xxmax);
       }
       else{
-         f3=new TF3(fname, fname);
-         return Fit(f3,option,goption,xxmin,xxmax);
+         TF3 f3(fname, fname);
+         return Fit(&f3,option,goption,xxmin,xxmax);
       }
    }
 
diff --git a/hist/hist/src/TMultiGraph.cxx b/hist/hist/src/TMultiGraph.cxx
index aa1e618f3cd..03e995252d1 100644
--- a/hist/hist/src/TMultiGraph.cxx
+++ b/hist/hist/src/TMultiGraph.cxx
@@ -481,14 +481,13 @@ TFitResultPtr TMultiGraph::Fit(const char *fname, Option_t *option, Option_t *,
 {
    char *linear;
    linear= (char*)strstr(fname, "++");
-   TF1 *f1=0;
-   if (linear)
-      f1=new TF1(fname, fname, xmin, xmax);
-   else {
-      f1 = (TF1*)gROOT->GetFunction(fname);
-      if (!f1) { Printf("Unknown function: %s",fname); return -1; }
+   if (linear) { 
+      TF1 f1(fname, fname, xmin, xmax);
+      return Fit(&f1,option,"",xmin,xmax);
    }
-
+   TF1 * f1 = (TF1*)gROOT->GetFunction(fname);
+   if (!f1) { Printf("Unknown function: %s",fname); return -1; }
+   
    return Fit(f1,option,"",xmin,xmax);
 }
 
-- 
GitLab