Skip navigation.
 
mlRe: Core Data and retain count
FROM : Bill
DATE : Tue Mar 04 22:46:45 2008

On Mar 4, 2008, at 1:06 PM, Ben Trumbull wrote:
> Bill,
>

>> On Mar 2, 2008, at 5:35 PM, Ben Trumbull wrote:

>>>> My question is, why would changing a property value cause another
>>>> property to have its retain count increase?

>>>
>>> No idea.  Why don't you run it in gdb and break on the -retain 
>>> method and get some stack traces ?
>>> This works best if the class you're debugging (in this case the 
>>> value window controller) has a custom retain/release method that 
>>> you  can break on instead of NSObject's etc.  If it doesn't, you 
>>> can  trivially make one by adding a category on it and putting -
>>> retain/-
>>> release/-autorelease methods on it that simply call super.

>>
>> Good idea.  Here's the stack trace:
>> #0    0x000029aa in -[NSWindowController(MainWindow) retain] at 
>> MainWindowController.m:35
>> #1    0x92dfa378 in -[NSManagedObject(_NSInternalMethods) 
>> _newPropertiesForRetainedTypes:andCopiedTypes:preserveFaults:]
>> #2    0x92dfa250 in -[NSManagedObject(_NSInternalMethods) 
>> _allProperties]
>> #3    0x92dfa185 in -
>> [NSManagedObjectContext(_NSInternalChangeProcessing) 
>> _establishEventSnapshotsForObject:]
>> #4    0x92dfd516 in _PFFastMOCObjectWillChange
>> #5    0x92dfb4e3 in -[NSManagedObject willChangeValueForKey:]
>> #6    0x00021a6c in -[Window setOpenAtLaunch:] at Window.m:117
>>
>> Not sure what to make of it, nor why setting the property 
>> openAtLaunch increments the retain count for the window controller 
>> property.

>
> At a low level, what's going on is that you're dirtying a managed 
> object and Core Data is taking a snapshot of the previous state for 
> this user event.  The snapshot supports undo, rollback, and the 
> merge policies, among other odds and ends.  This snapshot may be 
> pushed onto the undo stack, or simply kept around until you -save:, -
> rollback, or -reset the NSManagedObjectContext
>
> At a high level, you've created a retain cycle between the M & V 
> layers of the MVC pattern.
>
> After some thought, it seems a bit weird to make the 
> NSWindowController an attribute of a managed object.  Technically, 
> you could solve this problem by making it an unmodeled property 
> (ivar).  However, this is probably a symptom of a larger design issue.
> --
>
> -Ben


Ben,

  I have since made the NSWindowController an ivar, and everything now 
works much better.
Thank you for your help.

-Bil

Related mailsAuthorDate
mlCore Data and retain count Kimo Mar 2, 21:32
mlre: Core Data and retain count Ben Trumbull Mar 3, 02:35
mlRe: Core Data and retain count Bill Mar 3, 04:50
mlRe: Core Data and retain count Ben Trumbull Mar 4, 22:06
mlRe: Core Data and retain count Bill Mar 4, 22:46