Dear all,
I’ve been using ROOT for a few years but it has not been clear yet for me
how ROOT manages objects.
I understand that objects derived from TNamed are named and one can
retrieve their pointers by indicating the name as key, using
TDirectory::FindOjbect() method and so on.
Now my question is,
the objects (in memory) are allowed to have same name and be owned by identical
directory or not.
I am using v3.10/01 on Linux.
Followings are a part of TH1::Build() function.
if (fgAddDirectory && gDirectory) {
TH1 hold = (TH1)gDirectory->GetList()->FindObject(GetName());
if (hold) {
Warning(“Build”,“Replacing existing histogram: %s (Potential memory lea
k).”,GetName());
gDirectory->GetList()->Remove(hold);
// delete hold;
}
gDirectory->Append(this);
fDirectory = gDirectory;
It seems that
when one attempt to create new histogram, another object with same
name is removed from object list of current directory.
(but not deleted. Is the old object accessible?)
But following examle shows that building new object(h_new) with
existing name(“h_name”) is warned but the old object(h_old) is still
belong to “test.root”, although pointer of h_old can no longer
be retrived by TDirectory::FindObject().
root [0] TFile f(“test.root”, “new”)
root [1] TH1F h_old(“h_name”, “title”, 10, 0, 10)
root [2] TH1F ptr = &h_old
root [3] ptr
(class TH1F)0x89c2758
root [4] h_old.GetDirectory()->GetName()
(const char* 0x89c19a4)"test.root"
root [5] gDirectory->FindObject(“h_name”)
(const class TObject*)0x89c2758
root [6]
root [6] TH1F h_new(“h_name”, “title”, 100, 0, 100)
Warning in TH1::Build: Replacing existing histogram: h_name (Potential memory leak).
root [7] ptr
(class TH1F*)0x89c2758
root [8] gDirectory->FindObject(“h_name”)
(const class TObject*)0x8a29000
root [9] ptr->GetDirectory()->GetName()
(const char* 0x89c19a4)"test.root"
root [10] ptr = &h_new
(class TH1F*)0x8a29000
root [11] ptr->GetDirectory()->GetName()
(const char* 0x89c19a4)"test.root"
root [12] f.cd()
(Bool_t)1
root [13] gDirectory->FindObject(“h_name”)
(const class TObject*)0x8a29000
root [15] h_old.GetDirectory()->GetName()
(const char* 0x89c19a4)“test.root”
I am confused.
Could anyone explain this please?
Best regards,
Kame