user4951
user4951

Reputation: 33090

Why I ocassionally got this error?

NSString* className = NSStringFromClass([self class]);

if (!allTheSingletons)
{
    allTheSingletons = NSMutableDictionary.dictionary;
}

id result = allTheSingletons[className];

enter image description here

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

enter image description here

This is the declaration of allTheSingletons

static NSMutableDictionary * allTheSingletons;

Upvotes: 0

Views: 48

Answers (2)

KudoCC
KudoCC

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

uchuugaka
uchuugaka

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

Related Questions