Memory leak with basic TTree

Hi @CorentinH,

thanks for following up and providing the full reproducer!

I think you have false positives from valgrind here. If I run your code in a look 1000 times and print the memory consumption, I can still see no leak:

#include <TSystem.h>
#include <TFile.h>
#include <TTree.h>

#include <iostream>
#include <stdlib.h>

struct Hit {
  UShort_t label = 0;  //Hit label
  Float_t nrj = 0;     //energy
  Int_t nrj2 = 0;      //used if QDC2
  Int_t nrj3 = 0;      //used if QDC3
  Int_t nrj4 = 0;      //used if QDC4
  ULong64_t time = 0;  //time
  Bool_t pileup = 0;   //pile-up detection
  UInt_t coinc = 0;    //Hit label
  UShort_t mult = 0;   //Multiplicity of the hit
  Float_t nrjcal = 0;
};

void recreateFile() {
  Hit hit;
  srand48(time(0));
  TFile *file = new TFile("datafile.root", "recreate");
  TTree *tree = new TTree("DataTree", "DataTree");
  tree->Branch("nrj", &hit.nrj);
  tree->Branch("label", &hit.label);
  tree->Branch("time", &hit.time);
  for (int i = 0; i < 2000; i++) {
    hit.nrj = drand48() * 5;
    hit.time = drand48() * 5;
    hit.label = drand48() * 5;
    tree->Fill();
  }
  file->cd();
  tree->Write();
  file->Write();
  file->Close();
  delete file;
}

void repro() {
  ProcInfo_t pinfo;
  for (std::size_t i = 0; i < 10000; ++i) {
    recreateFile();

    if (i % 100 == 0) {
      gSystem->GetProcInfo(&pinfo);
      std::cout << i << " memory usage " << pinfo.fMemResident << " "
                << pinfo.fMemVirtual << std::endl;
    }
  }
}

Is there a way other than valgrind that you can observe the leak with? See also this write-up by Axel about why valgrind gives you false positives with ROOT:

If you can’t get the suppression file from $ROOTSYS, you can also find it here in the GitHub repository.

Hope this helps!
Jonas