Rule Definition
If singleton is invoked in a multi-threaded program, you could end up creating multiple instances of the class which will make the application unstable.
Remediation
Make sure that [[[self class] alloc] init] is done into a dispatch thread.
Violation Code Sample
+ (instancetype)sharedInstance {
static id sharedInstance = nil;
sharedInstance = [[[self class] alloc] init];
return sharedInstance;
}
Fixed Code Sample
+ (instancetype)sharedInstance {
static id sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedInstance = [[[self class] alloc] init];
});
return sharedInstance;
}
Reference
https://developer.apple.com/library/content/documentation/General/Conceptual/DevPedia-CocoaCore/Singleton.html#//apple_ref/doc/uid/TP40008195-CH49-SW1
Related Technologies
Technical Criterion
Secure Coding - API Abuse
About CAST Appmarq
CAST Appmarq is by far the biggest repository of data about real IT systems. It's built on thousands of analyzed applications, made of 35 different technologies, by over 300 business organizations across major verticals. It provides IT Leaders with factual key analytics to let them know if their applications are on track.