Re: [Pyobjc-dev] Bus Error involving NSFormatter and a method with returns by reference
Brought to you by:
ronaldoussoren
From: Bob I. <bo...@re...> - 2006-03-08 17:09:42
|
On Mar 8, 2006, at 8:10 AM, Phil Frost wrote: > Using PyObjC from SVN revision 1785, I have an implementation > NSFormatter as below that I think is triggering a bus error. I > wonder if > I am not implementing getObjectValue:forString:errorDescription: > correctly since it has two return-by-reference parameters and I could > not find any previous example. > > 1: class UPCFormatter(NSFormatter): > 2: def stringForObjectValue_(self, product): > 3: if not product: > 4: return '' > 5: else: > 6: return str(product.valueForKey_('upc')) > 7: > 8: def getObjectValue_forString_errorDescription_(self, upc): > 9: print self, upc > 10: > 11: if not upc: > 12: print 'no upc' > 13: return True, None, None > 14: > 15: context = NSApp().delegate().managedObjectContext > 16: products = fetchx(context, 'Product', 'upc = %@', [upc]) > 17: if len(products) > 1: > 18: print 'multiple products' > 19: return False, None, NSString.stringWithString_ > ('Multiple products with the UPC "%s" were found.' % (upc,)) > 20: elif len(products) == 0: > 21: print 'no products' > 22: return False, None, NSString.stringWithString_('No > products with the UPC "%s" were found.' % (upc,)) > 23: > 24: print 'matched %r' % (products[0],) > 25: return True, products[0], None > 26: > 27: getObjectValue_forString_errorDescription_ = objc.selector > (getObjectValue_forString_errorDescription_, signature='c@:o^@@o^@') > > I have tried this implementation without line 27, and also returning > python string literals instead of NSString instances. The behaviour > seemed unchanged in all cases. Line 27 doesn't do anything in this case because PyObjC already knows about this selector (and it uses the same signature as you're specifying manually). It's useless to manually specify the same signature that's already known on the superclass: >>> Foundation.NSFormatter.getObjectValue_forString_errorDescription_.signat ure 'c@:o^@@o^@' My guess is that for whatever reason the reference counting is getting borked somewhere. I'd send a retain/autorelease to the to be returned NSString.. and if that doesn't work, I'd try sending just a retain and see if that works (which would probably leak). If the latter is necessary, you'd have to intelligently cache and clear the results so that either exactly one string gets leaked and/or the cache of returned strings is cleared when the NSTextField goes away. -bob |