Re: [Pyobjc-dev] the mysterious attribute error
Brought to you by:
ronaldoussoren
From: Bob I. <bo...@re...> - 2004-03-28 19:52:45
|
On Mar 28, 2004, at 2:26 PM, Ronald Oussoren wrote: > On 28-mrt-04, at 18:36, Burris T. Ewell wrote: > >> Another clue: >> >> *** malloc[5538]: error for object 0x5b9f00: Double free >> >> Somewhere, memory management is going awry. > > That's consistent with my diagnosis. I'm thinking about a fix for > this, but I have not yet found a good solution for this. > > The problem is that __dict__ and attributes introduced by __slots__ > are 'PyObject*' attributes when you look at the object from > Objective-C. These are copied by the default copyWithZone: but without > updating the reference count (Py_INCREF-ing the value). That means > we're hosed when copy/copyWithZone: is used, because it is not > possible to fix the reference count using Python code. > > It is easy enough to silently introduce a copyWithZone: method that > does the right thing, but that won't work when someone tries to > implement a copyWithZone: method in Python (e.g. for customizing the > copy). > > When someone adds 'def copyWithZone_(self, zone)' to a new class, we > cannot insert the generic PyObjC-copyWithZone: implementation without > either adding serious hacks, e.g. looking for calls to the super-class > implementation of copyWithZone: and replacing them by calls to a > helper function, or changing the semantics. > > The best option seems to be to change the interface for copyWithZone_: > if you implement copyWithZone_ in Python it must have the following > interface: > > def copyWithZone_(self, zone, copy): > # Update copy here > return copy > > > I don't really like this, but don't see how we can have the normal > semantics and yet be able to insert our own method into the > call-chain. The reason I don't like this is that this interface > can/should only be used when subclassing a class that implements > NSCopying. > > Luckily we can give a usefull error-message when someone does > implement copyWithZone_ but doesn't use the right inteface. Why not just force a default copy and copyWithZone implementation (as far as Python is concerned), and instead use the __getstate__ and/or __reduce__ method of copying python things? -bob |