Re: [ROOT] Default Constructor

From: Rene Brun (Rene.Brun@cern.ch)
Date: Wed Jan 30 2002 - 11:16:16 MET


Hi Francois-Xavier,

Up to ROOT version 3.02/03 included, it was not possible to create
objects in the default constructor (I mean new objects created by the
object being instantiated). With these versions of ROOT, reading one object
implied doing something like
   Myclass *object = new MyClass();
   object->Streamer(buffer);

In this case, Streamer will also create the sub-objects at the same pointer
address
than the original objects. When the destructor of Myclass was called, a seg
violation appeared.
Also the default constructor is called by operations like:
  - calling Dump, Inspect, 
  - creating a context menu
in order to build the class dictionary in memory.

In version 3.02/06 and 07, I have protected the case when an object is already
allocated (could also be a float*, int*, char*, etc). If a non-null pointer is
found, the object is automatically deleted when streaming in a new object.
So, with the new version, you can create new objects in the default constructor.
However, I would like to discourage this practice for performance reasons.
When you are in read mode, it is useless to create an object that is going to be
automatically deleted when reading from the buffer.
The performance penalty does not occur when the object is in a top branch
as it is the case for the Event example.
Version 3.02 also includes the option "->" in the data member field
of the class declaration. If you decide to create a sub-object in the class
constructor and you guarantee that the corresponding pointer is never null,
ROOT will merely not delete the object but instead use the direct form
    subobject->Streamer(buffer);
instead of the more expansive form 
    buffer >> mysubobject;

I know that this should be better explained in the Users Guide.

Rene Brun



GENTIT Francois-Xavier DAPNIA wrote:
> 
>   Dear rooters,
>   In the documentation, p275, it is said:
> 
>   ROOT object I/O requires every class to have a default constructor...Be
> sure you do not allocate any space for embedded pointer in the default
> constructor.
> 
>   Now, if one looks at the Event.cxx example provided in root/test there are
> 2 "new" in the default constructorof the class Event?
> 
>           F.X. Gentit
>           DAPNIA/SPP CEN Saclay
>           tel : 01 69 08 30 38     fax : 01 69 08 64 28
>           web : http://home.cern.ch/~gentit/



This archive was generated by hypermail 2b29 : Sat Jan 04 2003 - 23:50:40 MET