Re: TH1::Build(), memory leakage

From: Rene Brun <brun_at_pcroot.cern.ch>
Date: Mon, 4 Jul 2005 16:17:33 +0200 (MEST)


Hi,

On Mon, 4 Jul 2005, Margar Simonyan wrote:

>
> in "Object Ownership" chapter it is written that histograms are owned by
> gDirectory (default) . I create TH1 object with new, it is added to
> gDirectory list, if I call delete operator, then we have double delete
> problem, since gDirectory will try to delete object, is it right ?

No, if you delete an histogram in a directory, it is automatically removed from the directory.

> Another related question. In THStack documentation it is
> written, "The THStack owns the objects in the list". What it means ?
> I create TH1 object with new, add to stack with
> THStack::Add(). Should I delete TH1 object with delete operator. I suppose
> no, since it is owned by THStack.

THStack clearly stpulates that it owns the histogram. In this case you should not try to delete the histogram yourself.

Rene brun

>
> Margar Simonyan
>
> On Mon, 4 Jul 2005, Rene Brun wrote:
>
>> When you create an object (eg TH1) with new, it is up to you to manage
>> this object.
>> See chapter about "Object Ownership" in the Users Guide
>>
>> Rene Brun
>>
>> On Sun, 3 Jul 2005, Margar Simonyan wrote:
>>
>>> Dear users of ROOT
>>>
>>> I would like to know why in TH1::Build() delete hold is
>>> commented. If one have a function which crates histogram with new then
>>> there is no (easy) way to avoid memory leakage.
>>>
>>> Best regards,
>>> Margar Simonyan
>>>
>>> if (hold) {
>>> Warning("Build","Replacing existing histogram: %s (Potential
>>> memory leak).",GetName());
>>> gDirectory->GetList()->Remove(hold);
>>> hold->SetDirectory(0);
>>> // delete hold;
>>> }
>>>
>>
>
Received on Mon Jul 04 2005 - 16:17:38 MEST

This archive was generated by hypermail 2.2.0 : Tue Jan 02 2007 - 14:45:10 MET