0 retain count


#1

When Xcode analyzes Homepwner it flags initWithZone: as returning a zero retain count. Does this merely mean, if called, this method is not adding to the the retain count of the existing singleton defaultStore whose retain count remains at something other than 0?

I see from querying that the retain count of defaultStore is -1. I see the utility of this but where in the code did this happen?

Thanks,
Byrne


#2

-1 is the signed interpretation of the max unsigned long (NSUIntegerMax), which is returned by our overridden version of -retainCount.

By convention, the analyzer expects ‘alloc’ methods to return something that’s retained. You can eliminate the warning by following Apple’s sample code for a Singleton (see the end of the “Cocoa Objects” section). The part that matters here is this:

+ (id) allocWithZone: (NSZone *) zone
{
    return [[self defaultStore] retain];

    // was: return [self defaultStore];
}

Adding the retain msg doesn’t actually affect the retain count because we’ve overridden -retain to just return self. The analyzer is just so happy to see the retain msg that it doesn’t care what the method actually does.


#3

I see that now, thanks. I’m still getting used to these methods we override but never call directly.