Skip to content

Instantly share code, notes, and snippets.

@nacho4d
Last active December 31, 2015 20:28
Show Gist options
  • Select an option

  • Save nacho4d/8039912 to your computer and use it in GitHub Desktop.

Select an option

Save nacho4d/8039912 to your computer and use it in GitHub Desktop.
leaking vs non-leaking singleton
// LEAKING
@implementation CandidatesWindow
CandidatesWindow *sharedInstance = nil;
- (id)initWithFrame:(CGRect)frame
{
if (sharedInstance) {
sharedInstance.frame = frame;
} else {
sharedInstance = [super initWithFrame:frame];
}
return sharedInstance;
}
@end
// NON-LEAKING
@implementation CandidatesWindow
+(CandidatesWindow *)sharedInstance {
static dispatch_once_t pred;
static CandidatesWindow *shared = nil;
dispatch_once(&pred, ^{
shared = [[CandidatesWindow alloc] init];
});
return shared;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment