Freeing memory created when using Clone()

Hello Rooters!

This may be a simple question, but it was acurring to me from time to time and up to now i was not able to figure out myself the question:

“How do you delete the memory on objects which were created with Clone() function?”

To get more into detail, here:

I have a loop which is called —a lot— of times, in this loop I do want to Clone a histogram in the way:

spectrum_interim = new TH2F();	spectrum_interim=(TH2F*)HistBG[h]->Clone();
// Do something with this histogram with TSpectrum2 Fitter...
delete spectrum_interim;

The longer I run this loop, the more memory is taken by my compiled program, so it seems to me that “spectrum_interim” is deleted, but there is sill some memory, induced by Clone() left, which is not freed.

What can I do with this? is there a way to “delete” the Clone memory??? (I am not familiar with this buffer stuff yet…)

Thanks in advance,
So it seems to me that


the code you sent is fine, so the leak is probably somewhere else. You can e.g. use valgrind; see for some info. Or you can use the object statistics tool of ROOT; see the users guide.

Cheers, Axel.

Yes your code must have a leak as you are creating teh TH2F object twice.

spectrum_interim = new TH2F(); spectrum_interim=(TH2F*)HistBG[h]->Clone(); by


Yikes, sorry, I didn’t read your code snipped carefully enough!