Re: [ROOT] Constructor limitation

From: Rene Brun (Rene.Brun@cern.ch)
Date: Mon Jul 07 2003 - 12:40:36 MEST


Hi Paolo,

Paolo ADRAGNA wrote:
> 
> Hi Rene,
> 
> thanks for the clear explanation. There is still a
> doubt I would like to expose you just to see if I completely understood
> the problem.
> In principle if I do not write MyClass on disk I could not take care of
> what you said. But if I'm not wrong, Streamers are automatically generated
> during dictionary cretion (both with rootcint and ACLiC), so that it is
> better to apply the rules you explained in all cases.
> 
> Is it correct?

yes. As a general rule
 -always initialize your pointers to 0 in the default constructor
 -except the case where you specify "->"

Rene Brun

> 
> Thanks,
> 
>                                         Paolo
> 
> On Mon, 7 Jul 2003, Rene Brun wrote:
> 
> > Hi Paolo,
> >
> > This is not a limitation, it is a question of principle.
> >
> > When reading a class object (say MyClass) from a file, Root calls the default
> > constructor
> >   MyClass *a = new MyClass();
> > then reads the object from the TBuffer b via
> >   a->Streamer(b)
> >
> > If MyClass contains a pointer to, eg
> >  AnotherClass *p;
> > MyClass::Streamer will call the default constructor of AnotherClass
> > to read the object, etc.
> > There are two special cases.
> >  -if in MyClass, the declaration for AnotherClass is
> >    AnotherClass *p //! this member is transient only
> >   you can allocate p in the default constructor of MyClass
> >
> >  -if in MyClass, the declaration for AnotherClass is
> >    AnotherClass *p;  //->
> >    you SHOULD create p in your MyClass default constructor. The symbol "->"
> >    in the comment field instructs ROOT that p is always created. In this case
> >    p->Streamer(b) is called directly.
> >
> > Note that in the normal case, Root will automatically delete p if p is not null.
> > However this can generate a memory leak is p is a collection of objects.
> >
> > Rene Brun
> >
> >
> > Paolo ADRAGNA wrote:
> > >
> > > Hello everyone,
> > >
> > > I would like to know if it is still valid the foollowing limitation in
> > > ROOT (as explained at page 290 of ROOT Guide): there is no possibility to
> > > allocate space for embedded pointer objects in the default constructors,
> > > because this space will be lost  (memory leak) while reading the object.
> > >
> > > Is all this still true or in the meantime something changed?
> > >
> > > Thank a lot,
> > >
> > >                                 Paolo Adragna
> > >
> >



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