Re: [ROOT] Memory allocation in default constructor

From: Rene Brun (
Date: Wed Jul 09 2003 - 21:45:36 MEST

Hi Dario,

The default constructor is automatically called by ROOT to:
  - create an instance and invoke the Streamer function when reading
   an object from a file
  - build the dictionary when required by several tools (Inspect, 
   DrawClass, Browse, etc).

You can detect when your default constructor is called in these special 
cases by testing
  if (TClass::IsCallingNew()) {
    //you are in the special case
    //do not allocate objects inside this constructor
  } else {
   // do what you want

Rene Brun

Wed, 9 Jul 
2003 wrote:

> Dear rooters,
> in the root documentation and elsewhere you recommend not to allocate
> memory in the default constructors, to avoid memory leaks.
> I've followed the "Event" example in the test directory, where "control"
> static variables (fgTracks) are defined for the initializations, such that
> memory is allocated just once.
> Now my code has got more complicated and I need to call in the same
> program several times object constructors to define different objects of
> the same class and I need to allocate memory for each of them.
> I understand that it is possible to introduce as many static pointers as
> the number of calls (like: fgTracks1, fgTracks2, fgTracks3...) and stop
> allocating new memory when all the static variables have been initialized.
> But doing so one loses in generality: I have to know in advance how many
> different objects of each class I will need, whereas this number
> can be different case by case...
> Can you suggest a clever and elegant solution to this problem?
> At the moment I've allocated memory in the constructor and checked whether
> in my typical applications I've memory leaks. It doesn't seem to be the
> case...
> Thanks and best regards,
> Dario Motta

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