Repository: rootpy/rootpy

Issue:
Segfault when closing a canvas
Hi,

I'm on e3aee45355eced5dd46f450bc88391a7b6c0062a.  Every time I Draw from a tree and subsequently close the canvas I get a segmentation violation.  Here is a sample session:

```
In [1]: from rootpy.tree import Tree
In [2]: from rootpy.io import File
In [3]: rfile = File('data/smalltree-really-new-MC-pre-PID-DsK.root')
In [4]: ftree = rfile.Get('ftree')
In [5]: ftree.Draw('hMom.Pt():PIDK', 'time<1', 'prof', markercolor=9, markerstyle=2)
Out[5]: Profile('htemp')
In [6]: INFO:rootpy.logger] | No default log handler configured. See `logging` module |
INFO:rootpy.logger] \    To suppress: 'rootpy.log.basic_config_colorized()'   /
ERROR:ROOT.TUnixSystem.DispatchSignals] segmentation violation
ERROR:stack] File "/usr/lib64/python2.7/threading.py", line 524, in __bootstrap
ERROR:stack]   self.__bootstrap_inner()
ERROR:stack] File "/usr/lib64/python2.7/threading.py", line 551, in __bootstrap_inner
ERROR:stack]   self.run()
ERROR:stack] File "/usr/lib64/python2.7/threading.py", line 504, in run
ERROR:stack]   self.__target(*self.__args, **self.__kwargs)
ERROR:stack] File "/usr/lib64/python2.7/site-packages/ROOT.py", line 339, in _processRootEvents
ERROR:stack]   gSystemProcessEvents()

[...]

The lines below might hint at the cause of the crash.
If they do not help you then please submit a bug report at
http://root.cern.ch/bugs. Please post the ENTIRE stack trace
from above as an attachment in addition to anything else
that might help us fixing this issue.
===========================================================
#6  0x0000000000000020 in ?? ()
#7  0x00000038e504cb2a in Close (this=0x358b5f0) at /usr/src/debug/root-5.34.07/graf2d/gpad/src/TPad.cxx:894
#8  TPad::Close (this=0x358b5f0) at /usr/src/debug/root-5.34.07/graf2d/gpad/src/TPad.cxx:884
#9  0x00000038e506dc2f in TCanvas::Close (this=0x358b5f0, option=0x38e50c6940 "") at /usr/src/debug/root-5.34.07/graf2d/gpad/src/TCanvas.cxx:743
#10 0x00000038e506d804 in TCanvas::Destructor (this=this
entry=0x358b5f0) at /usr/src/debug/root-5.34.07/graf2d/gpad/src/TCanvas.cxx:644
#11 0x00000038e506d867 in TCanvas::~TCanvas (this=0x358b5f0, __in_chrg=<optimized out>) at /usr/src/debug/root-5.34.07/graf2d/gpad/src/TCanvas.cxx:615
#12 0x00000038e506d929 in TCanvas::~TCanvas (this=0x358b5f0, __in_chrg=<optimized out>) at /usr/src/debug/root-5.34.07/graf2d/gpad/src/TCanvas.cxx:616
===========================================================


ERROR:ROOT.TUnixSystem.DispatchSignals] segmentation violation
```

The terminal also hangs and I have to force kill it.  I see this behaviour with both python and ipython.  For now I will revert to an older commit.  Please let me know if you need more information.

Cheers,

Segfault when closing a canvas
Hi,

I'm on e3aee45355eced5dd46f450bc88391a7b6c0062a.  Every time I Draw from a tree and subsequently close the canvas I get a segmentation violation.  Here is a sample session:

```
In [1]: from rootpy.tree import Tree
In [2]: from rootpy.io import File
In [3]: rfile = File('data/smalltree-really-new-MC-pre-PID-DsK.root')
In [4]: ftree = rfile.Get('ftree')
In [5]: ftree.Draw('hMom.Pt():PIDK', 'time<1', 'prof', markercolor=9, markerstyle=2)
Out[5]: Profile('htemp')
In [6]: INFO:rootpy.logger] | No default log handler configured. See `logging` module |
INFO:rootpy.logger] \    To suppress: 'rootpy.log.basic_config_colorized()'   /
ERROR:ROOT.TUnixSystem.DispatchSignals] segmentation violation
ERROR:stack] File "/usr/lib64/python2.7/threading.py", line 524, in __bootstrap
ERROR:stack]   self.__bootstrap_inner()
ERROR:stack] File "/usr/lib64/python2.7/threading.py", line 551, in __bootstrap_inner
ERROR:stack]   self.run()
ERROR:stack] File "/usr/lib64/python2.7/threading.py", line 504, in run
ERROR:stack]   self.__target(*self.__args, **self.__kwargs)
ERROR:stack] File "/usr/lib64/python2.7/site-packages/ROOT.py", line 339, in _processRootEvents
ERROR:stack]   gSystemProcessEvents()

[...]

The lines below might hint at the cause of the crash.
If they do not help you then please submit a bug report at
http://root.cern.ch/bugs. Please post the ENTIRE stack trace
from above as an attachment in addition to anything else
that might help us fixing this issue.
===========================================================
#6  0x0000000000000020 in ?? ()
#7  0x00000038e504cb2a in Close (this=0x358b5f0) at /usr/src/debug/root-5.34.07/graf2d/gpad/src/TPad.cxx:894
#8  TPad::Close (this=0x358b5f0) at /usr/src/debug/root-5.34.07/graf2d/gpad/src/TPad.cxx:884
#9  0x00000038e506dc2f in TCanvas::Close (this=0x358b5f0, option=0x38e50c6940 "") at /usr/src/debug/root-5.34.07/graf2d/gpad/src/TCanvas.cxx:743
#10 0x00000038e506d804 in TCanvas::Destructor (this=this
entry=0x358b5f0) at /usr/src/debug/root-5.34.07/graf2d/gpad/src/TCanvas.cxx:644
#11 0x00000038e506d867 in TCanvas::~TCanvas (this=0x358b5f0, __in_chrg=<optimized out>) at /usr/src/debug/root-5.34.07/graf2d/gpad/src/TCanvas.cxx:615
#12 0x00000038e506d929 in TCanvas::~TCanvas (this=0x358b5f0, __in_chrg=<optimized out>) at /usr/src/debug/root-5.34.07/graf2d/gpad/src/TCanvas.cxx:616
===========================================================


ERROR:ROOT.TUnixSystem.DispatchSignals] segmentation violation
```

The terminal also hangs and I have to force kill it.  I see this behaviour with both python and ipython.  For now I will revert to an older commit.  Please let me know if you need more information.

Cheers,



Discussion:
You don't see the segfault before e3aee45?

I confirm this problem... Endless segfaults! Every time I hit ENTER produces a new segfault...

For me the last known good commit is 1600661ce6f63514b84da88ea562c58edb9735a5.

Bisecting... 

So the bad commit is: 64793dd when I converted all classes over to the use of `super()`. Working on a fix...

You don't see the segfault before e3aee45?

I confirm this problem... Endless segfaults! Every time I hit ENTER produces a new segfault...

For me the last known good commit is 1600661ce6f63514b84da88ea562c58edb9735a5.

Bisecting... 

So the bad commit is: 64793dd when I converted all classes over to the use of `super()`. Working on a fix...


Resolving patch:
diff --git a/rootpy/plotting/canvas.py b/rootpy/plotting/canvas.py
--- a/rootpy/plotting/canvas.py
+++ b/rootpy/plotting/canvas.py
@@ -33,6 +33,8 @@ class Pad(_PadBase, QROOT.TPad):
 
     def __init__(self, *args, **kwargs):
 
+        # trigger finalSetup
+        ROOT.kTRUE
         super(Pad, self).__init__(*args, **kwargs)
         self._post_init()
 
@@ -55,6 +57,8 @@ def __init__(self,
             x = style.GetCanvasDefX()
         if y is None:
             y = style.GetCanvasDefY()
+        # trigger finalSetup
+        ROOT.kTRUE
         super(Canvas, self).__init__(x, y, width, height,
                                      name=name, title=title)
         self._post_init()
diff --git a/rootpy/plotting/core.py b/rootpy/plotting/core.py
--- a/rootpy/plotting/core.py
+++ b/rootpy/plotting/core.py
@@ -8,6 +8,7 @@
 import ROOT
 from .canvas import Canvas
 from ..decorators import chainable
+from ..memory.keepalive import keepalive
 
 
 def dim(thing):
@@ -1111,9 +1112,8 @@ class Color(_StyleContainer):
     def __init__(self, color):
         _StyleContainer.__init__(self, color, convert_color)
 
-from rootpy import QROOT
-from rootpy.util.hook import classhook, super_overridden
-from rootpy.memory.keepalive import keepalive
+from .. import QROOT
+from ..util.hook import classhook, super_overridden
 
 # TODO(pwaller): Make this a longer list of classes
 @classhook(QROOT.TH1, QROOT.TLegend, QROOT.TLatex)
@@ -1123,7 +1123,6 @@ def Draw(self, *args, **kwargs):
         keepalive(ROOT.gPad.func(), self)
         return super(DrawableKeepAlive, self).Draw(*args, **kwargs)
 
-
 if __name__ == "__main__":
     import doctest
     doctest.testmod()
diff --git a/rootpy/tree/tree.py b/rootpy/tree/tree.py
--- a/rootpy/tree/tree.py
+++ b/rootpy/tree/tree.py
@@ -728,6 +728,9 @@ def Draw(self,
             if isinstance(hist, Plottable):
                 hist.decorate(**kwargs)
 
+            # ROOT, don't try to delete this object! (See issue #277)
+            hist.SetBit(ROOT.kCanDelete, False)
+
             if 'goff' not in options:
                 keepalive(hist, pad)
                 pad.Modified()


Test patch: