Skip to content

Instantly share code, notes, and snippets.

@numist
Created May 21, 2013 22:32
Show Gist options
  • Save numist/5623807 to your computer and use it in GitHub Desktop.
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.
// 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