[Pythonmac-SIG] Memory errors implementing an NSCell to show colors in an NSTableView

Josh Marshall josh.p.marshall at gmail.com
Mon Jul 17 22:53:53 CEST 2006


I have been trying to get an application using PyObjC to display  
colors in an NSTableView. In my searching around, it seems the  
consensus is "NSColorWell isn't an NSCell, create your own subclass  
of NSCell which just shows the color."

So, with no further information, I tried this with an NSCell subclass:

class MyColorCell(NSCell):
     currentColor = objc.ivar('currentColor')

     def setObjectValue_(self, objectValue):
         self.currentColor = objectValue

     def drawInteriorWithFrame_inView_(self, cellFrame, controlView):
         if self.currentColor is not None:
             self.currentColor.drawSwatchInRect_(cellFrame)

and in my app delegate I have:

class ColorCellExampleAppDelegate(NibClassBuilder.AutoBaseClass):
     colors = objc.ivar('colors') # array containing created model  
objects
     colorArrayController = objc.ivar('colorArrayController') # bound  
so we can use addObject_()
     newColorWell = objc.ivar('newColorWell')
     colorTableColumn = objc.ivar('colorTableColumn') # use to set  
the data cell.

     def applicationDidFinishLaunching_(self, notification):
         self.colorCell = MyColorCell.alloc().init()
         self.colorTableColumn.setDataCell_(self.colorCell)

     def addNewColor_(self, sender): # bound to add button
         newColor = MyModelColorValue.alloc().initWithColor_ 
(self.newColorWell.color())
         self.colorArrayController.addObject_(newColor)

The model object definition is:
class MyModelColorValue(NSObject):
     color = objc.ivar('color')

     def initWithColor_(self,  color):
         self.color = color.copy()
         return self

The problem comes once I try to click on the colors in the list, the  
program just barfs. It looks like an object is getting freed where it  
shouldn't.
I'm getting either
objc: FREED(id): message isKindOfClass: sent to freed object=0x11cd510
or just a plain "Bus error" before it dies.

Looking at the stack trace, the first few lines are:
0   <<00000000>> 	0xfffeff18 objc_msgSend_rtp + 24
1   com.apple.AppKit               	0x93905594 -[NSValueBinder  
displayValueForObjectValue:] + 204
2   com.apple.AppKit               	0x93904b7c -[NSValueBinder  
_adjustObject:mode:observedController:observedKeyPath:context:editableSt 
ate:adjustState:] + 968
3   com.apple.AppKit               	0x9392fd04 -[NSValueBinder  
updateTableColumnDataCell:forDisplayAtIndex:] + 140
4   com.apple.AppKit               	0x939304d0 -[NSTextValueBinder  
updateTableColumnDataCell:forDisplayAtIndex:] + 88
5   com.apple.AppKit               	0x9392fc5c -[_NSBindingAdaptor  
tableColumn:willDisplayCell:row:] + 108
6   com.apple.AppKit               	0x9378d7c4 -[NSTableView  
_sendDelegateWillDisplayCell:forColumn:row:] + 88
7   com.apple.AppKit               	0x9378d204 -[NSTableView  
_drawContentsAtRow:column:clipRect:] + 396
8   com.apple.AppKit               	0x9378cbf8 -[NSTableView  
drawRow:clipRect:] + 220

I have a sample project demonstrating the error, but I can't attach  
it for some reason, so if you want it, email me.. I would appreciate  
it if someone can tell what is going on, or can point me towards  
getting started in debugging memory errors in PyObjC.

Thanks,
Josh



More information about the Pythonmac-SIG mailing list