Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSManagedObject retain count is 2 after creation

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?

like image 706
Bart Jedrocha Avatar asked Oct 19 '10 13:10

Bart Jedrocha


1 Answers

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...

like image 168
jv42 Avatar answered Oct 05 '22 11:10

jv42