THStack kCanDelete does not work

Hi rooters,
is a THStack drawn in a TCanvas deleted the same way as a TH1F by using the kCanDelete bit?

Unfortunately I have to follow up on an earlier thread about memory leaks which was solved by using the kCanDelete bit.
[url]Question on memory leak

It appears that a THStack with a set kCanDelete bit is not deleted with the Canvase.
I tried to work around with TH1F->Draw(“SAME”). In this case the memory was as expected free at the end.

Are there any explanations for this behaviour?

Thanks in advance for your input.


Could the observed behaviour be related to the fact that the THStack does not own the objects in the list?

Hi tc3t,
thanks for your awnser.
I was aware of this THStack feature. I tried to pass it, by setting the kCanDelete bits of all TH1F in the THStack and the THStack to true. I was hoping that the TH1Fs will nevertheless be deleted when the TCanvas is drawn. Apparently this is not the case.

Coming back to the original problem:
Is there a way to delete the TH1Fs belonging to a THStack drawn in a TCanvas, when the TCanvas is deleted?

Thanks for your feedback.


Hi rooters,
is there no posibility to set the kCanDelete bit in a way that the TH1Fs added to a THStack are deleted with the TCanvas?
Thanks for the help.

THStack is only a list of histograms, it does not own them. I do not think he can delete them because the histograms inside the THStack displayed in the TCanvas you want to delete, can also be displayed on other TCanvas(es) individually.

Hi Couet,
thanks for your answer.

I understand that the THStack does not own the TH1Fs inside, thus they are not deleted automatically when the THStack is deleted. My question is: Is there a way to tell the program that I want to delete them anyway when the TCanvas or THStack is delete? I was thinking about using the kCanDelete or SetOwner() but unfortunately this doesn’t work.

The argumentation that the TH1F might be displayed individually somewhere else is also comprehensible.
That’s in my opinion the reason to use the kCanDelete bit, to tell the program that I want the TH1Fs deleted when the TCanvas is deleted. BTW it is working like that if I am not using the THStack but instead add all the TH1Fs with Draw(“SAME”). Unfortunately this way I can not use the functions provided by THStack.

A similar case are TGraphs added to a TMultigraph which owns the TGraphs thus they are deleted when the TMultiGraph is deleted. Also here it might be possible that someone wants to display the TGraphs individually which is not possible after the TMultiGraph is deleted.

Thanks for your help


Have you tried:



Hi Couet,
thanks again for your answer.
I just tried what you suggested but it didn’t work right away.

I still have to call

at the end to actually remove the TH1Fs from the list.
This is weird as TLists are cleared in the deconstructor. Which should happen when the THStack is deleted.

As it seams to be easier to delete a couple of THStacks by hand than a couple times X histograms I will probably go for this solution.
It’s not nice but should work.

Thanks again for all the feedback. Let me know if you come up with a better solution.