Memory leak using TF1 function

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

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

Cheers,
Enrico

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

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

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;                                                                     
}

`

The original issue is unrelated to your problem.

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

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.”