Skip to content

Commit 8e8fb53

Browse files
ferdymercurylinev
authored andcommitted
[graf] Warn user about automatic overwrite of TCutG if same name
As done also in TDirectory::Append or when constructing a TH1 See https://root-forum.cern.ch/t/resolved-report-bug-lack-of-warning-with-crash-for-multiple-tcutg-object-having-the-same-name/62774/5
1 parent fb1eaee commit 8e8fb53

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

graf2d/graf/src/TCutG.cxx

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,12 @@ TCutG::TCutG(const char *name, Int_t n)
123123
fObjectX = nullptr;
124124
fObjectY = nullptr;
125125
SetName(name);
126-
delete gROOT->GetListOfSpecials()->FindObject(name);
126+
auto obj = gROOT->GetListOfSpecials()->FindObject(name);
127+
if (obj) {
128+
Warning("TCutG","Replacing existing %s: %s (Potential memory leak).",
129+
obj->IsA()->GetName(),obj->GetName());
130+
delete obj;
131+
}
127132
gROOT->GetListOfSpecials()->Add(this);
128133

129134
// Take name of cut variables from pad title if title contains ":"
@@ -164,7 +169,12 @@ TCutG::TCutG(const char *name, Int_t n, const Float_t *x, const Float_t *y)
164169
fObjectX = nullptr;
165170
fObjectY = nullptr;
166171
SetName(name);
167-
delete gROOT->GetListOfSpecials()->FindObject(name);
172+
auto obj = gROOT->GetListOfSpecials()->FindObject(name);
173+
if (obj) {
174+
Warning("TCutG","Replacing existing %s: %s (Potential memory leak).",
175+
obj->IsA()->GetName(),obj->GetName());
176+
delete obj;
177+
}
168178
gROOT->GetListOfSpecials()->Add(this);
169179

170180
// Take name of cut variables from pad title if title contains ":"
@@ -205,7 +215,12 @@ TCutG::TCutG(const char *name, Int_t n, const Double_t *x, const Double_t *y)
205215
fObjectX = nullptr;
206216
fObjectY = nullptr;
207217
SetName(name);
208-
delete gROOT->GetListOfSpecials()->FindObject(name);
218+
auto obj = gROOT->GetListOfSpecials()->FindObject(name);
219+
if (obj) {
220+
Warning("TCutG","Replacing existing %s: %s (Potential memory leak).",
221+
obj->IsA()->GetName(),obj->GetName());
222+
delete obj;
223+
}
209224
gROOT->GetListOfSpecials()->Add(this);
210225

211226
// Take name of cut variables from pad title if title contains ":"

0 commit comments

Comments
 (0)