The warning "deleting canvas with same name"

Hi All,

Where is “the same name” stating in the warning for Canvases or histograms ! ,and some of warnings give “potential memory leak” !

My Warnings ;

Warning in TCanvas::Constructor: Deleting canvas with same name: GAINCorrelation
Warning in TCanvas::Constructor: Deleting canvas with same name: GAIN Correlation in different DBs at HVs
Warning in TCanvas::Constructor: Deleting canvas with same name: GAIN Correlation in different HVs at DBs
Warning in TCanvas::Constructor: Deleting canvas with same name: Ch vs Gain in different DBs at 650V
Warning in TCanvas::Constructor: Deleting canvas with same name: Ch vs Gain in different DBs at 700V
Warning in TCanvas::Constructor: Deleting canvas with same name: GAIN vs HV
Warning in TCanvas::Constructor: Deleting canvas with same name: GAIN Correlation vs HVs
Warning in TFile::Append: Replacing existing TH1: 650GainVsChannel (Potential memory leak).
Warning in TFile::Append: Replacing existing TH1: 700GainVsChannel (Potential memory leak).

Names of my canvases, histograms, trees and ntuples;

(don’t try to run it !, it’s just a template of them!)


void Analysis1() {

TTree *tree1 = new TTree("GAIN for HVs and DBs","GAIN for HVs and DBs");
TCanvas *c5 = new TCanvas("GAIN Correlation in different DBs at HVs","GAIN Correlation in different DBs at HVs",600,400);
TCanvas *c6 = new TCanvas("GAIN Correlation in different HVs at DBs","GAIN Correlation in different HVs at DBs",600,400);
TH2D *GAIN650 = new TH2D("GAIN Correlation in different DBs for 650","GAIN Correlation in different DBs for 650",24,0,2700000,24,0,2700000);
TH2D *GAIN700 = new TH2D("GAIN Correlation in different DBs for 700","GAIN Correlation in different DBs for 700",24,0,2700000,24,0,2700000);
TH2D *GAINHV2 = new TH2D("GAIN Correlation in different HVs at DB1","GAIN Correlation in different HVs at DB1",48,0,2700000,48,0,2700000);
TH2D *GAINHV1 = new TH2D("GAIN Correlation in different HVs at DB0","GAIN Correlation in different HVs at DB0",48,0,2700000,48,0,2700000);

}

void Analysis2() {

TCanvas *c7 = new TCanvas("GAINCorrelation","GAINCorrelation",600,400);
TTree *tree2 = new TTree("Tree of GAINs for HVs","The Tree of gain of pmt's for HVs");
TNtuple *ntuple = new TNtuple("Ntuple of GAINs for HVs","GAIN for HVs","GAIN650:GAIN700");
TH2F *GAINforHV = new TH2F("GAINCorrelation in different HVs","GAINCorrelation in different HVs",96,0,2700000,96,0,2700000);

}

void AnalysisMAIN() {

TCanvas *c1 = new TCanvas("Ch vs Gain in different DBs at 650V","Ch vs Gain in different DBs at 650V",600,400);
TCanvas *c2 = new TCanvas("Ch vs Gain in different DBs at 700V","Ch vs Gain in different DBs at 700V",600,400);
TCanvas *c3 = new TCanvas("GAIN vs HV","GAIN vs HV",600,400);
TCanvas *c4 = new TCanvas("GAIN Correlation vs HVs","GAIN Correlation vs HVs",600,400);


TTree *tree = new TTree("Tree of GAINs","The Tree of Anaylsises");
TNtuple *ntuple = new TNtuple("Ntuple of GAINs","GAIN","GAIN");
TNtuple *ntuple1 = new TNtuple("Ntuple of GAINs for 650","ntuple for pmt's gain","Gain650");
TNtuple *ntuple2 = new TNtuple("Ntuple of GAINs for 700","ntuple for pmt's gain","Gain700");

TH2F *GAIN_650 = new TH2F("GAIN vs 650V","GAIN vs 650V",48,500,800,48,0,2700000);
TH2F *GAIN_700 = new TH2F("GAIN vs 700V","GAIN vs 700V",48,500,800,48,0,2700000);
TH2F *GAINHV = new TH2F("GAIN vs HV","GAIN vs HV",96,500,800,96,0,2700000);
TH3F *GAINHVCorr = new TH3F("GAIN Correlation vs HVs","GAIN Correlation vs HVs",96,100000,2700000,96,100000,2700000,96,500,800);
TH3F *GAINHVCorr1 = new TH3F("GAIN Correlation vs 650","GAIN Correlation vs 650",96,100000,2700000,96,100000,2700000,96,500,800);
TH3F *GAINHVCorr2 = new TH3F("GAIN Correlation vs 700","GAIN Correlation vs 700",96,100000,2700000,96,100000,2700000,96,500,800);

stringstream voltage_db;
  stringstream voltage;
  for(const int hv=0;hv<2;hv++) {
    for(const int db=0; db<2; db++) {
      voltage_db<<HV[hv]<<"_"<<db<<"GainVsChannel";
      voltage<<HV[hv]<<"GainVsChannel";
      HistGain[hv][db]= new TH2F(voltage_db.str().c_str(),voltage_db.str().c_str(),48,0,50,1000,0,2700000);
      HistGains[hv]= new TH2F(voltage.str().c_str(),voltage.str().c_str(),48,0,50,1000,0,2700000);
      voltage_db.str("");
      voltage.str("");
    }
  }


}

Thanks for your help,
Cheers,
Ersel

Hi Ersel,

concerning the deletion of canvases of the same name - do you execute the Analysis* functions multiple times?

Concerning the replacement of existing histograms and the potential leak - could it be that this line

HistGains[hv]= new TH2F(voltage.str().c_str(),voltage.str().c_str(),48,0,50,1000,0,2700000);

is the problematic one? You re-create the histograms for each DB - even if they exist already. And that’s exactly what the warning is about.

Hope that helps,
Benedikt

What am i supposed to do to cope with this "potential memory leak problem " and the others ?

Should i use TCanvas destructor TCanvas::~TCanvas(); or THistogram destructor TH1::~TH1(); after written the canvases or histograms ?

Thanks for your help,
Cheers,
Ersel

Hi Ersel,

Warning in <TFile::Append>: Replacing existing TH1: 650GainVsChannel (Potential memory leak).In this case, for the leak, you have two choices, one is to use unique names for the histograms or you can manage the lifetime of the histogram yourself (i.e. eventually call delete on the pointer for each of them).

Since this happens in ‘TFile::Append’, it also means that only one of the histogram is attached to the TFile and if you rely on TFile::Write to store them, only one of them (with the same name) will be stored in the TFile. For this one, either use unique name for the histogram or call TFile::WriteObject for each of them.

Cheers,
Philippe.

Thanks Philipe,

Done by deleting ‘new pointers’ :smiley: