Memmory leak reading ntuple

Hi all,

I have a macro which I compile with some functions, using root_v5.10.
Most of the methods look like:

   Int_t func(TTree* ntuple, vector<TH1D*>& histos);

Where ntuple is an already filled tree.

Inside I create the histos:

  for(Int_t j = 0; j < 16; j++) {
    TString hname("h");
    hname += j;
    histos[j] = new TH1D(hname.Data(), hname.Data(), 500, 0, 2000);

Afterwards I set the branches and read the tree. The tree has 3 branches: adc, sum and asym. The “adc” branch has 32 leafs “ADC0” to “ADC31” all type Short_t. The “sum” is similar but with 16. The “asym” branch is similar, but is Float_t type. I set the branches with:

  Short_t adc[32];
  Short_t sumraw[16];
  Float_t asymraw[16];
  ntuple->SetBranchAddress("adc", adc);
  ntuple->SetBranchAddress("sum", sumraw);
  ntuple->SetBranchAddress("asym", asymraw);

To read the event I use:

  const Int_t nevts = (Int_t)ntuple->GetEntries();
  Int_t print_mod = nevts/10;
  for(Int_t j = 0; j < nevts; j++) {
    if(j % print_mod == 0) 
           cout << "Processing evt " << j << " of " << nevts << endl;
    // Use event data...
This function is called from another one where TTree* ntuple is retrieved.
Do you see anything wrong with this code?
What happens is that memmory allocation increases indefinetly and after some passages by similar functions the PC crashes!

Could you please specify what am I doing wrong?
Thanks in advance for everything!

  Best regards,

How many times is the section that you name " Inside I create the histos: " executed? Do you clean up the old histogram afterward?


That kind of function is accessed about 4 times, but the histogram vectors are different each time. Do you think it would help to make them TClonesArray?
For further security and ensurance I added the following line:

immediatelly before the line:

The program still crashes the PC at the same point… In fact no segmentation fault or any other problem is reported… It simply runs and runs until the PC crashes for good and nothing else responds: no mouse, no keyboard, nothing… I tried in different machines with the same outcome. With my “bad” programming I can now crash servers and other mochines remotely :slight_smile:
I hope someone can help and we can resolve this problem soon.


Well there is no obvious error in your code snippets (however I can imagine many ways where they could be put together wrongly :slight_smile: ).

I strongly recommend that you give valgrind a try (http:://, it will most likely pin-point the origin of the problem.

For us to be able to help in further we would need a more complete example
(full runnable example would be best :slight_smile: ).


It is already a very good help to know that these separate parts seem OK. I’ll go through evrything some more and if necessary produce a error reproducible example (might not be easy).
I’ll check the tool you have pointed.
Thanks once more!