tags:

views:

39

answers:

1

I'm wondering if someone could explain why the retain count of a newly created managed object is 2. Here is the code I'm working with:

NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
NSEntityDescription *entity = [[self.fetchedResultsController fetchRequest] entity];
Album *newAlbum = [NSEntityDescription insertNewObjectForEntityForName:[entity name] inManagedObjectContext:context];
NSLog(@"Album retain count: %d", [newAlbum retainCount]);

According to the documentation for the insertNewObjectForEntity method, it returns a newly created autoreleased object. So I would expect the retain count to be 1 instead of 2. Can someone explain?

+3  A: 

Retain count is not guaranteed to be accurate, you may have encountered such a case.

I've made a sample project in which an object alloc/init was having a retain count of two, then 0 when released, etc...

jv42
Not only is it not guaranteed to be accurate, the documentation states that "it is very unlikely that you can get useful information from (retainCount)".
Aderstedt