Skip to content

Instantly share code, notes, and snippets.

@alanf
Last active April 26, 2016 20:21
Show Gist options
  • Save alanf/970417a70df387bbda6e0b6679445f2d to your computer and use it in GitHub Desktop.
Save alanf/970417a70df387bbda6e0b6679445f2d to your computer and use it in GitHub Desktop.
An example of how to track / count live instances of a particular class.
@implementation LiveInstanceTracking
static NSHashTable *instances;
- (instancetype)init {
self = [super init];
if (self) {
if (!instances) {
instances = [[NSHashTable alloc] initWithOptions:NSHashTableWeakMemory capacity:1000];
}
[instances addObject:self];
}
return self;
}
- (void)dealloc {
[instances removeObject:self];
}
@end
@alanf
Copy link
Author

alanf commented Apr 26, 2016

Note: This is slightly simplified: use dispatch_onceto create the NSHashTable if in a threaded environment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment