Reading tree: memory leak

Dear all,

Reading my tree causes a memory leak. I reproduced this problem in a small test program:

get this tar file:

tar -zxvf tree.tgz
cd Tree

./Debug 1
creates and fills the tree. It will be stored in test.root
./Debug 0
reads the tree. The printout of the object table shows that the number of TRefArrays is growing. This is the reason for the memory leak.

Now have a look at, reading stage.
As you can see, I’m adding a track to the event object at each event. Note however that the number of tracks is constant, as it should be. Tracks are correctly deleted, but not the TRefArray owned by the track.

I tried to force a call to my destructors by doing:

TFile f("test.root");
TTree *t = (TTree*) f.Get("T");
Event* event = 0;
t->SetBranchAddress("event", &event);

for(int i=0; i<t->GetEntries(); i++) {
  delete event; event = 0;

but then, I get a segmentation violation in TTree::GetEntry

I probably miss something, but I’ve been fighting with this for so long now, that I’m getting blind to my mistakes.

I definitely need to be able to update the event at reading stage. Do you know how I could cure the memory leak ?
many thanks,


on SLC3

Why do you call
when you read? tree->GetEntry will do it automatically.


Hello Rene,

GetEntry will get the 100 tracks per event out of the tree.
then, I’m adding one track.

This is just a dummy example. My real tree is used for clustering purpose. The event contains an array of cells, an array of clusters made of cells and so on. The simulation program propagates the particles to the calorimeters, and produces a list of cells with an energy deposit, and does some clustering.
Cells and clusters are saved in the tree.

Then, out of the simulation program, I’m redoing the clustering, and add the new clusters to the event TClonesArray of clusters. This allows to treat old and new clusters in the same way, eg. for display.

All clusters (old and new ones) contain a TRefArray to cells, which is not deleted before reading a new event, whereas the clusters seem to be deleted correctly. Hence the memory leak.

Would you have a solution ?

thanks again,


Could you test your program with a recent version like 5.10?


just tested with:

the problem is still there.


Your class Track has a member TRefArray*. You must make sure that
this array is deleted when starting a new Event. Do:

for(int i=0; i<t->GetEntries(); i++) { event->Clear(); t->GetEntry(i); event->AddTrack(); gObjectTable->Print(); }

see also: … rray:Clear


Dear Rene,

Thanks a lot, it works for the test program, and I could cure part of the leak in the real one. I still have a small leak, which is under investigation.