refactoring ListCtrl caused segmentation fault

I’m refactoring a CheckListCtrl and am now getting a crash, “Segmentation fault (core dumped)”. The old code worked fine.

Old:

     self.list

= CheckListCtrl(self)
self.list.InsertColumn(0, ‘Name’, width=250)
self.list.InsertColumn(1, ‘Dose’, width=180)
self.list.InsertColumn
(2, ‘Tablets’, width=60)
self.list.InsertColumn(3, ‘Frequency’, width=80)
self.list.InsertColumn(4, ‘#’, width=40)
self.list.InsertColumn
(5, ‘Refills’, width=60)
self.list.InsertColumn(6, ‘Number’, width=0)
self.list.InsertColumn(7, ‘Date’, width=100)
meds = med_find(ptID)
for i in meds:
index = self.list.InsertStringItem(sys.maxint, i[‘med_name’])
self.list.SetStringItem
(index, 1, i[‘dose’])
self.list.SetStringItem(index, 2, str(i[‘number_tablets’]))

        self.list.SetStringItem(index, 3, i['frequency'])
        self.list.SetStringItem(index, 4, str(i['number_pills']))
        self.list.SetStringItem(index, 5, str(i['refills']))
self.list.SetStringItem(index, 6, str(i['med_number']))

self.list.SetStringItem(index, 7, str(i['script_date']))

New:

class CheckListCtrl(wx.ListCtrl, CheckListCtrlMixin, ListCtrlAutoWidthMixin):
def init(self, parent):
wx.ListCtrl.init(self, parent, -1, style=wx.LC_REPORT | wx.SUNKEN_BORDER)
CheckListCtrlMixin.init(self)
ListCtrlAutoWidthMixin.init(self)

def buildCheckListCtrl(instance, columns, data):
ctrl = CheckListCtrl(instance)
index = 0
for columnName, columnWidth in columns:
ctrl.InsertColumn
(index, columnName, width = columnWidth)
index = index + 1
for i in data:
keys = i.keys()
col = 1
newindex = ctrl.InsertStringItem(sys.maxint, i[keys[0]])
for k in keys[1:]:
ctrl.SetStringItem(newindex, col, str(i[k]))
col = col + 1

    return ctrl

I would love to understand why the change is causing a problem. I am new to Python and wxPython. Unforturnately I cannot find the core dump file on the system I am using, Ubuntu 7.04. Thanks in advance for any help.

Mike

Attached is a small file that duplicates the problem I’m having. Thanks for the help.

Mike

ListCtrl.py (1.83 KB)

···

On Jan 14, 2008 2:44 PM, Robin Dunn <robin@alldunn.com > wrote:

Michael Barron wrote:

I’m refactoring a CheckListCtrl and am now getting a crash,

“Segmentation fault (core dumped)”. The old code worked fine.

I would love to understand why the change is causing a problem. I am
new to Python and wxPython. Unforturnately I cannot find the core dump

file on the system I am using, Ubuntu 7.04. Thanks in advance for any help.

Which version of wxPython? Can you tell how far it gets before the
crash? My guess is that one of the mix-ins is doing something that

assumes that there are columns and/or items already in the listctrl, so
you may have to reorder some things. Can you make a small runnable
sample that shows the problem?


http://wiki.wxpython.org/MakingSampleApps


Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!


To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwidgets.org

For additional commands, e-mail: wxPython-users-help@lists.wxwidgets.org

Michael Barron wrote:

Attached is a small file that duplicates the problem I'm having. Thanks for the help.

You are trying to call SetStringItem for columns that don't exist. There is an assert being triggered in the wx code, but you're getting a segfault because the default Debian/Ubuntu packages are built without the debug flags turned on and so the C++ wx assert code is not compiled in to the binaries. You might want to try using the debug python and debug wxPython packages during development.

ListCtrl.py (2.46 KB)

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!

Thanks for the help Robin. In addition to checking to make sure the data I’m adding matches the number of columns, I dumped the dictionary cursor for the ordinary one. The random order of the dictionary was sending the wrong data to the columns.

Great chapter in the book on refactoring. My application is slowly getting closer to what a real programmer might produce.

Mike