Looping over THStack freezes ROOT 5.34.07, python 2.7.4

ROOT 5.34.07, python 2.7.4
This snippet of code

from ROOT import THStack,TH1F,TCanvas
hs=THStack('hs','stack')
c1=TCanvas('c1','Canvas',800,600)
for i in range(0,5):
    h=TH1F('h'+str(i),'Hist '+str(i),50,-1,1)
    h.FillRandom('gaus',1000)
    hs.Add(h)
for hist in hs.GetHists():
    h.GetName()
hs.Draw()
c1.SaveAs('bug.pdf')

Will sometimes run as expected, and other times it will hang
indefinitely.
Result of uname -a:

Linux atlxxx 2.6.18-274.3.1.el5 #1 SMP Tue Sep 6 18:52:56 EDT 2011 x86_64 x86_64 x86_64 GNU/Linux

Hi,

seems that the THStack is listening to itself going dodo-bird, coming from the TCanvas:#0 0x00007fcfa6109000 in THStack::RecursiveRemove(TObject*) () #1 0x00007fcfab17cf50 in TList::RecursiveRemove(TObject*) () #2 0x00007fcfa55f09e6 in TPad::RecursiveRemove(TObject*) () #3 0x00007fcfab17cf50 in TList::RecursiveRemove(TObject*) () #4 0x00007fcfab177dab in THashList::RecursiveRemove(TObject*) () #5 0x00007fcfab12e4f8 in TObject::~TObject() () #6 0x00007fcfab17e0b9 in TList::~TList() () #7 0x00007fcfa610a65c in THStack::~THStack() () #8 0x00007fcfa610a789 in THStack::~THStack() () #9 0x00007fcfab1c3049 in TClass::Destructor(void*, bool) ( #10 0x00007fcfac317f3f in PyROOT::op_dealloc_nofree(PyROOT::ObjectProxy*) ()
You can tell python not to keep ownership of the THStack, as the TCanvas will ensure its destruction:from ROOT import SetOwnership SetOwnership( hs, False )
As an alternative, doing a “del c1” at the end of the script to reverse the deletion order does the trick as well (in that case, python gets the notification instead of the TCanvas, and it does not propagate).

HTH,
Wim

Thanks for the pointers! I’ll try this to avoid hanging my code when it runs on older ROOT versions. (This doesn’t happen in 5.34.08 as far as I can tell).