Tuesday, May 22, 2012

NSCache getting released randomly

so I had a category of NSCache:



@interface NSCache (SharedCache)
+(NSCache*)sharedCache;
@end

#import "NSCache+SharedCache.h"

@implementation NSCache (SharedCache)
+(NSCache*)sharedCache;
{
static NSCache* sharedCache = nil;
if (sharedCache == nil) {
sharedCache = [[self alloc] init];
[sharedCache setCountLimit:0];
}
return sharedCache;
}

@end


However ocasionnaly this gets deallocated and therefore my app crashes. If I retain sharedCache this problem is solved. What is the best way to have a shared NSCache to void crashes that it gets deallocated randomly?





1 comment: