Create a gist now

Instantly share code, notes, and snippets.

#if 1
- (id)retain {
NSUInteger oldRetainCount = [super retainCount];
id result = [super retain];
NSUInteger newRetainCount = [super retainCount];
printf("%s<%p> ++retainCount: %lu => %lu\n", [[self className] UTF8String], self, oldRetainCount, newRetainCount);
printf("%s\n", [[[NSThread callStackSymbols] description] UTF8String]);
return result;
}
- (void)release {
NSUInteger oldRetainCount = [super retainCount];
BOOL gonnaDealloc = oldRetainCount == 1;
if (gonnaDealloc) {
printf("%s<%p> --retainCount: 1 => 0 (gonna dealloc)\n", [[self className] UTF8String], self);
printf("%s\n", [[[NSThread callStackSymbols] description] UTF8String]);
}
[super release];
if (!gonnaDealloc) {
NSUInteger newRetainCount = [super retainCount];
printf("%s<%p> --retainCount: %lu => %lu\n", [[self className] UTF8String], self, oldRetainCount, newRetainCount);
printf("%s\n", [[[NSThread callStackSymbols] description] UTF8String]);
}
}
#endif
#if 1
- (id)retain {
NSUInteger oldRetainCount = [super retainCount];
id result = [super retain];
NSUInteger newRetainCount = [super retainCount];
printf("%s<%p> ++retainCount: %d => %d\n %s", [[self className] UTF8String], self, oldRetainCount, newRetainCount, [[[NSThread currentThread] description] UTF8String]);
printf("%s\n", [[[NSThread callStackSymbols] description] UTF8String]);
return result;
}
- (void)release {
NSUInteger oldRetainCount = [super retainCount];
BOOL gonnaDealloc = oldRetainCount == 1;
if (gonnaDealloc) {
printf("%s<%p> --retainCount: 1 => 0 (gonna dealloc) %s\n", [[self className] UTF8String], self, [[[NSThread currentThread] description] UTF8String]);
printf("%s\n", [[[NSThread callStackSymbols] description] UTF8String]);
}
[super release];
if (!gonnaDealloc) {
NSUInteger newRetainCount = [super retainCount];
printf("%s<%p> --retainCount: %d => %d %s\n", [[self className] UTF8String], self, oldRetainCount, newRetainCount, [[[NSThread currentThread] description] UTF8String]);
printf("%s\n", [[[NSThread callStackSymbols] description] UTF8String]);
}
}
#endif
#include <objc/runtime.h>
#if 1
static char sAutoreleaseCountKey;
- (id)retain {
NSUInteger oldRetainCount = [super retainCount];
id result = [super retain];
NSUInteger newRetainCount = [super retainCount];
NSUInteger autoreleaseCount = 0;
NSData *autoreleaseCountObj = objc_getAssociatedObject(self, &sAutoreleaseCountKey);
if (autoreleaseCountObj) {
autoreleaseCount = *(NSUInteger*)[autoreleaseCountObj bytes];
}
NSUInteger effectiveRetainCount = newRetainCount - autoreleaseCount;
printf("%s<%p> retain: retainCount:%lu => %lu (%lu)\n",
[[self className] UTF8String],
self,
oldRetainCount,
newRetainCount,
effectiveRetainCount);
printf("%s\n", [[[NSThread callStackSymbols] description] UTF8String]);
return result;
}
- (id)autorelease {
NSUInteger oldRetainCount = [super retainCount];
id result = [super autorelease];
NSUInteger newRetainCount = [super retainCount];
NSMutableData *autoreleaseCountObj = objc_getAssociatedObject(self, &sAutoreleaseCountKey);
if (!autoreleaseCountObj) {
NSUInteger zero = 0;
autoreleaseCountObj = [NSMutableData dataWithBytes:&zero length:sizeof(zero)];
objc_setAssociatedObject(self,
&sAutoreleaseCountKey,
autoreleaseCountObj,
OBJC_ASSOCIATION_RETAIN);
}
NSUInteger *autoreleaseCount = (NSUInteger*)[autoreleaseCountObj mutableBytes];
(*autoreleaseCount)++;
NSUInteger effectiveRetainCount = newRetainCount - *autoreleaseCount;
printf("%s<%p> autorelease: retainCount:%lu => %lu (%lu)\n",
[[self className] UTF8String],
self,
oldRetainCount,
newRetainCount,
effectiveRetainCount);
printf("%s\n", [[[NSThread callStackSymbols] description] UTF8String]);
return result;
}
- (void)release {
NSArray *callStack = [NSThread callStackSymbols];
BOOL isAutoreleasing = NO;
for (NSString *stackSymbol in callStack) {
if ([stackSymbol rangeOfString:@" _CFAutoreleasePoolPop "].location != NSNotFound) {
isAutoreleasing = YES;
break;
}
}
const char *method = isAutoreleasing ? "release (auto)" : "release (manual)";
NSUInteger oldRetainCount = [super retainCount];
BOOL gonnaDealloc = oldRetainCount == 1;
if (gonnaDealloc) {
printf("%s<%p> %s: retainCount:1 => 0 (gonna dealloc)\n",
[[self className] UTF8String],
self,
method);
printf("%s\n", [[callStack description] UTF8String]);
}
[super release];
if (!gonnaDealloc) {
NSUInteger autoreleaseCount = 0;
if (isAutoreleasing) {
NSMutableData *autoreleaseCountObj = objc_getAssociatedObject(self, &sAutoreleaseCountKey);
if (autoreleaseCountObj) {
NSUInteger *autoreleaseCountPtr = (NSUInteger*)[autoreleaseCountObj mutableBytes];
(*autoreleaseCountPtr)--;
autoreleaseCount = *autoreleaseCountPtr;
}
}
NSUInteger newRetainCount = [super retainCount];
NSUInteger effectiveRetainCount = newRetainCount - autoreleaseCount;
printf("%s<%p> %s: retainCount:%lu => %lu (%lu)\n",
[[self className] UTF8String],
self,
method,
oldRetainCount,
newRetainCount,
effectiveRetainCount);
printf("%s\n", [[callStack description] UTF8String]);
}
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment