How to avoid memory leak working with vector pointer?

Hi all,

I have an ntuple with a tree that was created with std::vector. So to read it I should point the branches to a std::vector *variable. I’m having troubles with memory leak however… I don’t know how this kind of variable should be well worked. I believe that, since the vectors can have different sizes, when I call the next event (tree->GetEntry()) can some space of the memory keep allocated. Should I use some procedure inside the events loop to free memory? (Reminding I can not delete the pointer before finish the loop since the tree was referenced to it).

I have an strange update (at least for me)… I was using the code iteratively… I just call it inside ROOT. Now, if I compile the code (inside ROOT) the memory leak does not happen!! If someone understand that and also now how to work with those variables, please let some reply… I will appreciate.

See root.cern.ch/doc/master/classTB … 0018c446cb

If you have a pointer to a vector std::vector* vptr, then you have to delete the object after each time you call GetEntry or set the branch address, as it calls implicitly a “new”. Also, if the vector contains other pointers instead of ints, then you have to first delete all elements of the vector, and then the pointer.

std::vector<int> * vptr = NULL;
tree->SetBranchAddress("vec", &vptr);
delete vptr;

tree->GetEntry(1);
//Do sth with vptr
delete vptr;

tree->GetEntry(2);
//Do sth with vptr
delete vptr;

[quote=“ferhue”]See root.cern.ch/doc/master/classTB … 0018c446cb

If you have a pointer to a vector std::vector* vptr, then you have to delete the object after each time you call GetEntry or set the branch address, as it calls implicitly a “new”. Also, if the vector contains other pointers instead of ints, then you have to first delete all elements of the vector, and then the pointer.

[code]
std::vector * vptr = NULL;
tree->SetBranchAddress(“vec”, &vptr);
delete vptr;

tree->GetEntry(1);
//Do sth with vptr
delete vptr;

tree->GetEntry(2);
//Do sth with vptr
delete vptr;

[/code][/quote]

Thank you by your suggestion! I read previously a few that tutorial. But, as we can see there the pointer is deleted after the loop over the entries (because the tree branch was referenced to it). One can do as you said, make the reference and then delete the pointer for each event. But, that is not a efficient way… it takes more time (at least it was what I found). I’m still curious on why when the code is compiled this memory leak does not arise…

What is the exact code that shows the memory leak?

Philippe.

No idea why the leak is not present when compiled, it could be something similar to this:

sft.its.cern.ch/jira/browse/ROO … ment-68037

[quote=“ferhue”]No idea why the leak is not present when compiled, it could be something similar to this:

sft.its.cern.ch/jira/browse/ROO … ment-68037[/quote]

Thanks ferhue and Philippe by reply. I don’t think the problem is similar to that one…
I uploaded the code now, so you can have a look. I already got the results I need… but I would like to understand this problem to improve my programming abilities.
getNormalizedDeltas.C (12.4 KB)

I’d need also the ROOT file to check this.

Analysing -1 from file ../SingleMuonPU140/tracks_from_LTF_logic5b6.root
Error in <TFile::TFile>: file ../SingleMuonPU140/tracks_from_LTF_logic5b6.root does not exist

(Sorry I overlooked this answer back then.)