Re: deleting histograms

Rene Brun (Rene.Brun@cern.ch)
Thu, 18 Mar 1999 08:09:55 +0000


Richard Dubois wrote:
>
> I would like to allow for cleaning up histograms, so I loop over the
> objects in gDirectory->GetList() and make a TObjArray of those
> inheriting from TH1 (is there a better way?). Then from this list I can
> Reset() them or delete them.
>
> If I delete them, I find that, when I redefine the same histogram, it
> appears to go through the fill OK, but when I go to draw it, I get a seg
> fault.
>
> I'm wondering if Root keeps its own list of histograms and doesn't
> realize that I have deleted it.
>
> Is there a safe way to delete the histograms?
>

Hi Richard,
very likely, you are deleting your histograms twice!
By default TH1 and TTree objects are automatically added to the list
of objects "gDirectory->Getlist()" associated with the current directory
in memory. You do not need to enter them into a new list.
If you persist in storing your histograms into your own list and you
delete these histograms, you MUST remove them from the original list.
You can remove an histogram from the gDirectory list in two ways.
h->SetDirectory(0);
or
gDirectory->GetList()->Remove(h);

Rene Brun