how to handle segmentation fault

I am using Python3 with self-build wxPhoenix(3.0.3.dev1548.5774184 gtk2
(phoenix)).
I am quite new to Python and now have segmentation fault while I tried
an wxPython-example.

I post the code at the end of this mail but it doesn't really matter.
What I am asking for is how to handle such segmentation fault messages?
In the past I debugged C++ software with high integrated
clicki-colorfull IDEs - very nice.
Now I don't know how to debug with Python. I am using geany as "IDE".
I even don't know how to read the "core dump". :smiley:

How should I handle such a situation now and in the future.

btw: The code
[python]
#!/usr/bin/env python3
import wx
import wx.grid

class LineupTable(wx.grid.GridTableBase):
     data = (("CF", "Bob", "Dernier"), ("2B", "Ryne", "Sandberg"),
            ("LF", "Gary", "Matthews"), ("1B", "Leon", "Durham"))

     colLabels = ("Last", "First")

     def __init__(self):
          wx.grid.GridTableBase.__init__(self)

     def GetNumberRows(self):
          return len(self.data)

     def GetNumberCols(self):
          return len(self.data[0]) - 1

     def GetColLabelValue(self, col):
          return self.colLabels[col]

     def GetRowLabelValue(self, row):
          return self.data[row][0]

     def IsEmptyCell(self, row, col):
          return False

     def GetValue(self, row, col):
          return self.data[row][col + 1]

     def SetValue(self, row, col, value):
          pass

class SimpleGrid(wx.grid.Grid):
      def __init__(self, parent):
          wx.grid.Grid.__init__(self, parent, -1)
          self.SetTable(LineupTable())

class TestFrame(wx.Frame):
    def __init__(self, parent):
        wx.Frame.__init__(self, parent, -1, "A Grid",
            size=(275, 275))
        grid = SimpleGrid(self)

if __name__ == '__main__':
    app = wx.App()
    frame = TestFrame(None)
    frame.Show(True)
    app.MainLoop()
[/python]

Hi,

I am using Python3 with self-build wxPhoenix(3.0.3.dev1548.5774184 gtk2
(phoenix)).
I am quite new to Python and now have segmentation fault while I tried
an wxPython-example.

I post the code at the end of this mail but it doesn't really matter.
What I am asking for is how to handle such segmentation fault messages?
In the past I debugged C++ software with high integrated
clicki-colorfull IDEs - very nice.
Now I don't know how to debug with Python. I am using geany as "IDE".
I even don't know how to read the "core dump". :smiley:

How should I handle such a situation now and in the future.

Hhm, see the same on Win 8.1 with Phoenix. I use WingIDE which has a debugger (there are others which do have a debugger, e.g. Boa - see also wxPythonPit Apps - wxPyWiki), so I set different breakpoints (guessing) until I found the culprit. I also looked at the GridCustTable.py sample to find differences.

For me the line:
self.SetTable(LineupTable())

needed to be changed to:
self.SetTable(LineupTable(), True)

Werner

···

On 2/8/2015 16:46, moonkid@posteo.org wrote:

Ok, this works.
The question is still what was the problem?
I didn't understand this second parameter at all but "False" should
work, too. ?

···

On 2015-02-08 18:10 Werner <wernerfbd@gmx.ch> wrote:

needed to be changed to:
self.SetTable(LineupTable(), True)

Hi,

needed to be changed to:
self.SetTable(LineupTable(), True)

Ok, this works.
The question is still what was the problem?
I didn't understand this second parameter at all but "False" should
work, too. ?

Yes, False should work too, but then you need to take care of cleaning up/deleting the table, IICU.

It should definitely not segfault, if at all it should be an exception.

There doesn't seem to be a ticket for this in trac, you might want to create one.
http://trac.wxwidgets.org/search?q=phoenix&noquickjump=1&ticket=on

Werner

···

On 2/8/2015 22:51, moonkid@posteo.org wrote:

On 2015-02-08 18:10 Werner <wernerfbd@gmx.ch> wrote:

Yes, False should work too, but then you need to take care of
cleaning up/deleting the table, IICU.

I am not sure if I understand it correct.

If it is 'True' the "View" would destroy/close the
GridTableBase-derived object if the View itself is closed?

So it should be 'False' if there are more then one "View" on that
GridTableBase, right?

How would I "take care" of deleting such a table? Isn't there a garbage
collector in Python? I would know what "deleting" means in C++ but not
in Python. :wink:

There doesn't seem to be a ticket for this in trac, you might want to
create one.

<http://trac.wxwidgets.org/ticket/16846&gt;

···

On 2015-02-09 08:48 Werner <wernerfbd@gmx.ch> wrote:

Hi,

Take my comments with a grain of salt, just a hobby programmer:-) .

Yes, False should work too, but then you need to take care of
cleaning up/deleting the table, IICU.

I am not sure if I understand it correct.

If it is 'True' the "View" would destroy/close the
GridTableBase-derived object if the View itself is closed?

that is my understanding.

So it should be 'False' if there are more then one "View" on that
GridTableBase, right?

I would guess no, but I have never done that. You might want to try on Py2.7 with a classic build of wxPython and see if it works there.

How would I "take care" of deleting such a table? Isn't there a garbage
collector in Python?

Yes there is, a bit of googling will give you better info then I could give.

  I would know what "deleting" means in C++ but not
in Python. :wink:

Normally things will gc'd when they go out of focus, e.g.:

def somemethod():
     a = 'some string'

'a' will get gc'd after the 'somemethod' ended as 'a' is local variable/attribute to the method.

If you want to force deletion then you would do:

del somevariable

Maybe this link explains things better.

Werner

···

On 2/9/2015 12:54, moonkid@posteo.org wrote:

On 2015-02-09 08:48 Werner <wernerfbd@gmx.ch> wrote:

moonkid@posteo.org wrote:

Yes, False should work too, but then you need to take care of
cleaning up/deleting the table, IICU.

I am not sure if I understand it correct.

If it is 'True' the "View" would destroy/close the
GridTableBase-derived object if the View itself is closed?

So it should be 'False' if there are more then one "View" on that
GridTableBase, right?

Yes that is the intent. Probably isn't used much though.

How would I "take care" of deleting such a table? Isn't there a garbage
collector in Python? I would know what "deleting" means in C++ but not
in Python. :wink:

For wxPython objects you have to keep in mind that there are python objects and there are C++ objects, and getting them to play together nicely is sometimes trickier than it seems. There is also the concept of object ownership that has to be dealt with when wrapping C++ objects with Python objects, because the C++ objects are not reference counted like Python objects, unless the reference counting is explicitly implemented in the class library. So when you create an object in Python that wraps a C++ object, is it owned by the Python object? By some other C++ object? By itself? Does that ownership ever change? (The answers are: any of the above, and often).

In this case the C++ wxGrid::SetTable gives us the option of having it take ownership of the table (meaning that it will destroy the object when it is done) or of letting the programmer manage it's lifetime. If you pass true then the grid will expect to be the only one who will destroy the table and there is a little bit of python code added that will tell the python proxy object that it does not own the table any more so it will not try to destroy it when the proxy object is garbage collected. However, when you passed False (the default) the proxy object will still be the owner of the C++ object, and so when the proxy is destroyed the C++ object is too, and then if the Grid tries to use it after that point it is a bad C++ pointer and like C++ usually does, it crashes.

Since you created, used, and didn't keep a reference to the table, like this:

     self.SetTable(LineupTable())

then Python's gc kicked in shortly thereafter and destroyed the proxy object which then destroyed the C++ object. If you instead hold a reference to the table like this:

     self.table = LineupTable()
     self.SetTable(self.table)

then it will not be gc'd until self is destroyed, and all will work as you expect it to.

···

On 2015-02-09 08:48 Werner<wernerfbd@gmx.ch> wrote:

--
Robin Dunn
Software Craftsman