If I have a class that has a sharedInstance of itself (singleton pattern) or a shared instance of another class, how do I make sure that shared instance is released properly when my program exits? Better yet, can you point me to some documentation about this?
sample class (with all other methods removed for clarity):
@interface Foo : NSObject {
}
+ (Foo*)sharedInstance;
@end
the .m file:
static Foo* SharedInstance;
@implementation Foo
+ (Foo*)sharedInstance
{
if (!SharedInstance)
SharedInstance = [[Foo alloc] init]; // possible memory leak?
return SharedInstance;
}
@end
In the above code, when can I release SharedInstance?