Here’s a simple class:
#import "One.h"
#import "Two.h"
@implementation DataFileRegistrar
static NSMutableDictionary *elementToClassMapping;
+ (void)load
{
[self registerClass:[One class] forElement:@"one"];
[self registerClass:[Two class] forElement:@"two"];
}
+ (void)registerClass:(Class)class forElement:(NSString *)element
{
if (!elementToClassMapping) {
elementToClassMapping = [NSMutableDictionary dictionaryWithObject:class forKey:element];
} else {
[elementToClassMapping setValue:class forKey:element];
}
}
+ (id)classForElement:(NSString *)element
{
return [elementToClassMapping valueForKey:element];
}
@end
The problem is this compiler message:
objc[7172]: Object 0x6840720 of class __NSCFDictionary autoreleased with no pool in place - just leaking - break on objc_autoreleaseNoPool() to debug
Any ideas what’s going on?
Basically, I want to have a simple class with a couple of class methods and one static dictionary. It would be always used without instantiation. I want to use it for a couple of things right after the app starts and then I want to release its memory. I thought ARC can take care of this.
Your class’
+loadis calling methods which callsautorelease.Your class is loaded before
main.You can explicitly create an autorelease pool in
+load:However, it’s often better to guarantee the order of your program’s initialization and load explicitly before creating any threads in
main: