How dangerous is removing an object from a gDirectory's list

I open a file and use the “Get” method to load an object from that file. I then want to close the file, but don’t want the object to be deleted, so I just use the TDirectory::GetList()->Remove(obj) to remove it from the directory list. As a result the object is not deleted.

But how dangerous is this? For example, if that object is a histogram then its fDirectory will still point to the (now closed) file. Are there operations one would normally perform on the histogram that will try to use fDirectory? A quick look at the source file for a histo makes me think now, but the GetDirectory() call on this histogram means anyone can use it.


For a histogram, you can use SetDirectory(0)


Indeed, same for a TTree and some other things. The problem is that I don’t know what type of object it is at that point. I just know I want to pull it out. :slight_smile: This means for histos it will no be reset to zero.

Hi Gordon,

You can also look and use the value returned by myobject->IsA()->GetDirectoryAutoAdd()


I see - and so I can use that to reset the value to zero, and that will take care of everything? Very nice!