RE: [ROOT] Root I/O and object deletion

From: Philippe Canal (pcanal@fnal.gov)
Date: Tue Apr 29 2003 - 19:48:14 MEST


> How does I (or root) know whether an object referenced by my object has
> been created by the user, the class itself or the root environment? And
> how do I make sure, that in any circumstances the objects are deleted
> exactly once?

Another alternative is to use a class like:

class A : public TObject {
    TNamed *fOwnedNamed;
    TRef    fNamed;
    A() : fOwnedNamed(0) {};
    ~A() { delete fOwnNamed; }
    void SetName(const char *name)
    { delete fOwnedNamed; fOwnedNamed = new TNamed(name, ""); fNamed =
fOwnedNamed; }
    vodi SetName(TNamed *name) { delete fOwnedNamed; fOwnedNamed = 0; fNamed
= name; }
    ClassDef(A, 1)
}

Where fOwnerNamed keeps track of whether the object belongs to the A or not
and the the TRef keeps track of the reference to the tnamed whether its is
own or not.

Cheers,
Philippe.

-----Original Message-----
From: owner-roottalk@pcroot.cern.ch
[mailto:owner-roottalk@pcroot.cern.ch]On Behalf Of Thomas Bretz
Sent: Tuesday, April 29, 2003 11:50 AM
To: Rene Brun
Cc: roottalk@pcroot.cern.ch
Subject: Re: [ROOT] Root I/O and object deletion


Rene,

this cannot be true. Root reads the two TNamed objects from a file. This
means, that root allocates memory for it. In other words, what you say
is: If one doesn't want to overload the Streamer function (make sure
that the Bit/Flag is Set) there is no way, that the TNamed object will
ever be deleted in the case of 'Obj1'.

If I now store more of these object in a tree, referencing the same
TNamed, root stores the TNamed only once (Am I correct?). Assume, that
in a program I created this TNamed is created by the user calling
SetName(new TNamed) and the pointer is distributed to the other class A
objects. If I now read such class A objects from a Tree I would, at the
end, delete the TNamed object several times...

How does I (or root) know whether an object referenced by my object has
been created by the user, the class itself or the root environment? And
how do I make sure, that in any circumstances the objects are deleted
exactly once?

Thomas.



Rene Brun wrote:
> Thomas,
>
> In one case you call SetBit, in the other one you call ResetBit!!
>
> Rene Brun
>
> Thomas Bretz wrote:
>
>>Dear rooters,
>>
>>here is a simplified example to illustrate my question: If I have a class
>>
>>class A : public TObject {
>>    TNamed *fNamed;
>>    ~A() { if (TestBit(BIT(14)) delete fNamed; }
>>    void SetName(const char *name)
>>    { fNamed = new TNamed(name, ""); SetBit(BIT(14)); }
>>    vodi SetName(TNamed *name) { fNamed = name; ResetBit(BIT(14)); }
>>    ClassDef(A, 1)
>>}>>
>>and now I create an instance of this class
>>A a, b;
>>a.SetName(new TNamed);
>>b.SetName("NewName");
>>
>>and write both objects to a file:
>>a.Write("Obj1");
>>b.Write("Obj2");
>>
>>now I read this object into new objects:
>>A c,d;
>>c.Read("Obj1");
>>d.Read("Obj2");
>>
>>As I understand it, now both objects (c and d) have a newly allocated
>>TNamed. But for one of the two objects BIT(14) is set and for one not.
>>How can I make sure, that the destruction of the TNamed-data-member is
>>done correctly in any case?
>>
>>Thanks in advance,
>>Thomas.
>
>


#################################################################
#################################################################
#################################################################
#####
#####
#####
#################################################################
#################################################################
#################################################################



This archive was generated by hypermail 2b29 : Thu Jan 01 2004 - 17:50:11 MET