# Memory leak using TF1 function

**URL:** <https://root-forum.cern.ch/t/memory-leak-using-tf1-function/34660>\
**Category:** Newbie\
**Created:** [June 18, 2019, 12:52pm UTC](https://root-forum.cern.ch/t/memory-leak-using-tf1-function/34660 "2019-06-18T12:52:20Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![pgreen](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/p/8797f3/32.png) [@pgreen](https://root-forum.cern.ch/u/pgreen)\
**Post date:** [June 18, 2019, 12:52pm UTC](https://root-forum.cern.ch/t/memory-leak-using-tf1-function/34660/1 "2019-06-18T12:52:20Z")

</div>

Hi,

I am having issues deleting TF1 functions that were created using new. Doing this repeatedly in a loop is causing a memory leak. For example:

void test() {  
for (int i = 0; i \< 100000; i++){  
TF1\* fGH\_tmp = new TF1(“fGH\_tmp”,gaisser\_hillas\_fit,0.,2000,4);   
delete fGH\_tmp;   
}  
}

where gaisser\_hillas\_fit.rootmacro.h is:

Double\_t gaisser\_hillas\_fit(Double\_t \*x, Double\_t \*par) {

//This is the Gaisser-Hillas function  
Double\_t X\_mu\_0=par[3];  
Double\_t Normalization=par[0];  
Double\_t Diff=par[1]-X\_mu\_0;  
Double\_t Term=pow((\*x-X\_mu\_0)/Diff,Diff/par[2]);  
Double\_t Exponential=TMath::Exp((par[1]-\*x)/par[2]);

return ( Normalization \* Term \* Exponential);  
}

Is there some additional step that needs to be taken to correctly delete the TF1\*?

Thanks

---

<div class="post-metadata">

**Author:** ![eguiraud](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/eguiraud/32/32217_2.png) [@eguiraud](https://root-forum.cern.ch/u/eguiraud)\
**Post date:** [June 18, 2019, 2:00pm UTC](https://root-forum.cern.ch/t/memory-leak-using-tf1-function/34660/2 "2019-06-18T14:00:14Z")

</div>

Hi @pgreen,  
thank you for the report!  
As far as I can tell, you found a bug! We are looking into it.

Cheers,  
Enrico

---

<div class="post-metadata">

**Author:** ![eguiraud](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/eguiraud/32/32217_2.png) [@eguiraud](https://root-forum.cern.ch/u/eguiraud)\
**Post date:** [June 19, 2019, 9:16am UTC](https://root-forum.cern.ch/t/memory-leak-using-tf1-function/34660/3 "2019-06-19T09:16:08Z")

</div>

This is now [https://sft.its.cern.ch/jira/browse/ROOT-10191](https://sft.its.cern.ch/jira/browse/ROOT-10191)

---

<div class="post-metadata">

**Author:** ![jtnattress](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/a3d4f5/32.png) [@jtnattress](https://root-forum.cern.ch/u/jtnattress)\
**Post date:** [May 6, 2022, 2:47pm UTC](https://root-forum.cern.ch/t/memory-leak-using-tf1-function/34660/4 "2022-05-06T14:47:54Z")

</div>

Was this issue fixed? I am having a similar issue in 6.24/02. I cannot delete the pointer without causing a seg fault.

```auto
TH1* makeGaus(double mean, double sigma, double theInt)                            
{                                                                                 
  TF1 *fg = new TF1("fg","gaus(0)",0,9.2992468);                                
  //nBins = (7-0) / binWidth;                                                   
  fg->SetNpx(4095);                                                             
  double height = 0.3989*theInt / sigma;                                        
  fg->FixParameter(0,height);                                                   
  fg->FixParameter(1,mean);                                                     
  fg->FixParameter(2,sigma);                                                    
  auto h = fg->GetHistogram();                                                  
  delete fg;                                                                    
  return h;                                                                     
}

```

`

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [May 6, 2022, 2:57pm UTC](https://root-forum.cern.ch/t/memory-leak-using-tf1-function/34660/5 "2022-05-06T14:57:15Z")

</div>

The original issue is unrelated to your problem.

Try with:  
`auto h = (TH1*)fg->GetHistogram()->Clone("h");`

---

<div class="post-metadata">

**Author:** ![jtnattress](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/a3d4f5/32.png) [@jtnattress](https://root-forum.cern.ch/u/jtnattress)\
**Post date:** [May 6, 2022, 4:42pm UTC](https://root-forum.cern.ch/t/memory-leak-using-tf1-function/34660/6 "2022-05-06T16:42:47Z")

</div>

That worked! Thank you.

I am assuming it has to do with something noted for the method, “It is then recommended either to clone the return object or calling again the GetHistogram function whenever is needed.”
