I have this singleton code in an application that I am updating now and then. While I took the Apple link code, I had to update it once or twice since clang complained and Apple updated it in response. Today I tried to analyze using Xcode 4.6, and again I get a warning, although Apple did not update after two years. I really cannot understand how they cannot get a singleton encoded in such a way that static analyzers will not complain. And this is not the case, as they cannot modify the static analyzer to place their code if they feel that their reference code is right. But back to the code, I have this class:
@implementation MySingleton
static MySingleton *sSharedSingleton = nil;
+ (MySingleton *)sharedSingleton
{
if (sSharedSingleton == nil)
{
sSharedSingleton = [[super allocWithZone:NULL] init];
}
return sSharedSingleton;
}
+ (id)allocWithZone:(NSZone *)zone
{
return [[self sharedSingleton] retain];
}
- (id)copyWithZone:(NSZone *)zone
{
return self;
}
- (id)retain
{
return self;
}
- (unsigned)retainCount
{
return NSUIntegerMax;
}
- (oneway void)release
{
}
- (id)autorelease
{
return self;
}
@end
, https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CocoaFundamentals/CocoaObjects/CocoaObjects.html
Clang allocWithZone :
Object with a +0 retain count returned to caller where a +1 (owning) retain count is expected
, , . , clang ? (, ) ?