Last active
December 31, 2015 20:28
-
-
Save nacho4d/8039912 to your computer and use it in GitHub Desktop.
leaking vs non-leaking singleton
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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