Reputation: 33090
NSString* className = NSStringFromClass([self class]);
if (!allTheSingletons)
{
allTheSingletons = NSMutableDictionary.dictionary;
}
id result = allTheSingletons[className];
All seems like very regular codes that can't go wrong. The variable allTheSingletons contain 7 key/value pairs, one of which is the className.
Even if the dictionary does not have className, key, which is BGMDCRManagedObjectContextThreadHandler it should simply return nil.
Yet what I got, occasionally is
This is the declaration of allTheSingletons
static NSMutableDictionary * allTheSingletons;
Upvotes: 0
Views: 48
Reputation: 6952
I'm not sure, but if these code will be executed in multiple threads, make it thread-safe.
static dispatch_once_t onceToken ;
dispatch_once(&onceToken, ^{
allTheSingletons = [NSMutableDictionary dictionary] ;
}) ;
Upvotes: 1
Reputation: 12782
What you can do is run instruments and see if something is getting dealloced and freed. It looks like you need to check if the dictionary is nil and return nil if it is. If it is not nil, you might check to see if it even has a key matching what you're asking for. If not, return nil.
If you don't want to return nil then you need to ensure that it IS not nil by creating it then accessing it if it is nil.
You might also make sure it is not only not nil when you access it but make sure you have a strong reference to it. Make it a strong property.
Upvotes: 1