[ROOT] Memory allocation in default constructor

From: Dario.Motta@mpi-hd.mpg.de
Date: Wed Jul 09 2003 - 20:07:24 MEST


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