Created
May 21, 2013 22:32
-
-
Save numist/5623807 to your computer and use it in GitHub Desktop.
Internal refCount for tracking object liveness in a way that avoids the object doing meaningful work when it's supposed to be dead/dying.
This file contains 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
// Example ignores concurrency protection on internalRefCount for brevity. | |
// Not compatible with ARC, for obvious reasons (but could be implemented in part with a non-ARC category?). | |
@interface Foo () | |
@property (nonatomic, assign) NSUInteger internalRefCount; | |
@end | |
@implementation Foo | |
- (instancetype)init; | |
{ | |
self = [super init]; | |
if (self) { | |
_internalRefCount = 1; | |
} | |
return self; | |
} | |
- (id)retain; | |
{ | |
self.internalRefCount++; | |
} | |
- (oneway void)release; | |
{ | |
if (!--self.internalRefCount) { | |
// Oh no! Dealloc is coming! | |
[self release]; | |
} | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment