TClonesArray : memory leak using

Following up on … =hauschild

I have class that contains a TClonesArray of another class :

class SamplePoint : public TObject {
Short_t fPoint;
ClassDef(SamplePoint, 1);

class Signal : public TObject {
UInt_t fTriggerCounter; //current event trigger number
Int_t fNSamples; // number of samples in this oscillogram
TClonesArray *fSamples; //-> array of SamplePoint

TClonesArray *GetArray() const {return fSamples;}
ClassDef(Signal, 1);

I can make and use a Tree in which “Signal” is made the branch-root and there is only one Signal per “event”.

I have a memory leak when I try and make a TClonesArray of Signal so I may have a variable number of Signals per “event”.

I am running ROOT 5.26/00 (tags/v5-26-00@31883, Dec 17 2009, 08:12:10 on macosx64) - macos x 10.6

Included are the .h and .cxx files for those of you who can explain
to me where I am leaking TClonesArrays. Basically I loose the address
of the TClonesArray.

Many thanks in advance,
Karl Hauschild
test7.tar.gz (6.01 KB)

In your function Signal::Clear, replace

fSamples->Clear("C"); //clear ? TClonesArray by

delete fSamples;

Dear Rene,

Thank you very much. That solved my leak.

Now for the next level of complication : A class to hold a merging of a variable number “Signal” events in a TClonesArray.


Hi Rene and Karl.

I don’t understand Rene’s suggestion here.

If I make Rene’s change to Karl’s code, then the function test_Signal_Tree breaks at run time. His test_FakeEvent_Tree runs fine with the “delete fSamples” line.

Also, Karl’s original call of Clear(“C”) is what the documentation tells us to do. Its ALSO what is done in the Event.cxx/.h in the $ROOTSYS/test directory.

I also seem to be having a memory issue when using a TClonesArray. I have an “Event” class with a pointer to 4 different TClonesArray (each containing an array of a different SubRecord). It looks to me that I am doing exactly what is being done in Event.cxx/h, yet my program allocates a large amount of memory. I just run a simple script in the cint that opens and fills a tree, just adding objects to only one of my 4 TClonesArrays. I’m calling Clear after every event. At first, ROOT allocates 20 MB, but after a ~minute, the memory allocation jumps up to 200 MB and then hovers there. If I change the script to add objects to all 4 TClonesArrays, it allocates up to 600 MB.

However, when I run the Event program in the ROOTSYS/test directory, it only allocates 90 MB. My next step will be to compile outside of ACLiC and see what I get.

Is this the normal behavior?

Calling Clear(“C”) is fine in case of the top level TClonesArray if the class
in the array in turn does not contain pointers to objects.
In this example, the top ClonesArray contains a class Signal containing a TClonesArray. This array must be deleted otherwise calling the Signal constructor at the same place will not delete the previous array and you will get a leak.
Note that the optimum use of a TClonesArray is for classes not containing other dynamic allocations as this defeats the original idea of this class ::slight_smile:



Some time ago I tried to do something similar with STL vectors, but
never managed to extract them out of the Tree in which they were
stored. This is why I went the TClonesArray route even though it is not
an optimal use. Unfortunately I can not find the old code using STL.

I must also admit that I am now having segmentation faults in part of the
code that I did not have before. I will have to see if I can work around this.

Thanks for the additional comments.