I am creating a TGraphErrors using the copy constructor from another TGraphErrors that already exists in memory, and I am getting warnings about potential memory leaks that I can’t understand:
TGraphErrors *gGMp_Ratio_newRC = new TGraphErrors( *gGMp );
gGMp_Ratio_newRC->SetName("gGMp_Ratio_newRC");
“gGMp” is a TGraphErrors that already exists “in memory” or technically in a ROOT file that has been opened for writing, that was already allocated using “new TGraphErrors(…)”.
I get the following warning when I create this graph:
Warning in TFile::Append: Replacing existing TH1: gGMp_Ratio_newRC (Potential memory leak).
It doesn’t appear to be causing any problems with any of the various objects being created and written to the file. However, I am a bit surprised by this behavior/warning because I never told ROOT to write this new object to the file and nothing else is done to it other than to loop over the points and use SetPoint(…) and SetPointError(…).
Some additional details: this warning only appears after the (compiled) macro completes its execution and control is returned to the interactive ROOT session…
It also appears regardless of whether and when I do or don’t write either the original TGraphErrors or the copied one to the file.
Can it be that you have a histogram with this name?
Before these two lines and also after them, try to add: gROOT->ls(); if (gFile) gFile->ls(); if (gPad) gPad->ls();
You could also try to use: TGraphErrors *gGMp_Ratio_newRC = ((TGraphErrors*)(gGMp->Clone("gGMp_Ratio_newRC")));
@Axel or anybody who cares. There is indeed a serious bug in the TGraph / TGraphErrors copy constructors in ROOT 6 (ROOT 5 is fine):
{
TFile *f = TFile::Open("trial.root", "RECREATE");
const Int_t n = 20;
Double_t x[n], y[n];
for (Int_t i = 0; i < n; i++) { x[i] = i*0.1; y[i] = 10*sin(x[i]+0.2); }
TGraph *g = new TGraph(n,x,y);
TCanvas *c = new TCanvas("c","A Simple Graph Example",200,10,700,500);
// the next two lines will create a histogram (for the frame) ...
g->Draw("AC*"); // ... but this histogram WILL NOT be stored in the file
c->Modified(); c->Update(); // make sure it's really (re)drawn
// this WILL NOT store the histogram in the file
TGraph *g2 = ((TGraph*)(g->Clone()));
// this WILL store the histogram in the file
TGraph *g3 = new TGraph(*g);
f->Write();
f->ls(); // ROOT 6 gives: "KEY: TH1F Graph;1 Graph"
delete f;
}
Tested on Ubuntu 20.04.2 LTS / x86_64, gcc 9.3.0, ROOT “v6-22-00-patches” and “v5-34-00-patches”.
AFTER the graph creation via copy constructor, it appears that there are two objects with the same name, one of which is a TH1F. Interestingly enough, by this stage the original TGraphErrors, gGMp, has already been written to the file and also drawn in one or more canvases. Could this be a symptom of the bug you identified below?
I confirm that the clone method gets rid of the warning and prevents the creation of the extra histogram. Seems like it would probably be a good idea to fix the behavior of the copy constructor though…