How to get rid of memory leak warning?

Hi,

in my code, I do the following ( TH1D *fHisto[2]; )

  TString histNameX = TString("histX_")+fFilenameNoExt;
  TString histNameY = TString("histY_")+fFilenameNoExt;
  fHisto[kX] = (TH1D*) gROOT->FindObject(histNameX);
  fHisto[kY] = (TH1D*) gROOT->FindObject(histNameY);

  if (fHisto[kX]) {delete fHisto[kX]; fHisto[kX] = NULL;}
  if (fHisto[kY]) {delete fHisto[kY]; fHisto[kY] = NULL;}                                           

  cout << "+++ before definition" << endl;
  fHisto[kX] = new TH1D(histNameX.Data(), histNameX.Data(),
                        nCol, static_cast<Double_t>(startCol-1), static_cast<Double_t>(endCol));
  fHisto[kY] = new TH1D(histNameY.Data(), histNameY.Data(),
                        nRow, static_cast<Double_t>(startRow-1), static_cast<Double_t>(endRow));
  cout << "+++ after definition" << endl;

i.e., I check with gROOT if my histogram has already been defined, then delete it to make sure there won’t be a memory leak. But ROOT keeps telling me I am producing a memory leak… the above code produces the following output:

+++ before definition
Warning in <TROOT::Append>: Replacing existing TH1: histX_DAQ20090520T125430_0 (Potential memory leak).
Warning in <TROOT::Append>: Replacing existing TH1: histX_DAQ20090520T125430_0 (Potential memory leak).
Warning in <TROOT::Append>: Replacing existing TH1: histX_DAQ20090520T125430_0 (Potential memory leak).
Warning in <TROOT::Append>: Replacing existing TH1: histY_DAQ20090520T125430_0 (Potential memory leak).
Warning in <TROOT::Append>: Replacing existing TH1: histY_DAQ20090520T125430_0 (Potential memory leak).
Warning in <TROOT::Append>: Replacing existing TH1: histY_DAQ20090520T125430_0 (Potential memory leak).
+++ after definition

What am I doing wrong?

Thanks for any advice,

Thomas

Hi,

your code doesn’t correspond to the error message, so I’ll have to guess a bit… First thing to check: are histNameX.Data() and histNameY.Data() really different? Second: maybe there are multiple hists with the same name - can you transform the if (fHisto[kX]) into a while((fHisto[kX] = gROOT->FindObject(histNameX))) loop?

Cheers, Axel.

Yes, the second guess was correct. Apparently I have several histograms with the same name to clean up.

Thanks a lot!

Thomas