Last active
April 26, 2016 20:21
-
-
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.
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
@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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note: This is slightly simplified: use
dispatch_once
to create theNSHashTable
if in a threaded environment.