Skip to content

Instantly share code, notes, and snippets.

@ccgus
Created August 5, 2011 00:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ccgus/1126639 to your computer and use it in GitHub Desktop.
Save ccgus/1126639 to your computer and use it in GitHub Desktop.
Weak Sauce
#import <Foundation/Foundation.h>
@interface FMFancyObject : NSObject {
}
@end
@interface FMDeallocCheckerObjectHolder : NSObject {
__weak id _holdingObject;
}
@property (weak) id holdingObject;
@end
@implementation FMDeallocCheckerObjectHolder
@synthesize holdingObject=_holdingObject;
@end
@implementation FMFancyObject
- (void)dealloc {
NSLog(@"FMFancyObject dealloc!");
[super dealloc];
}
@end
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
FMDeallocCheckerObjectHolder *objh = [FMDeallocCheckerObjectHolder new];
FMFancyObject *foo = [FMFancyObject new];
[objh setHoldingObject:foo];
NSLog(@"Releasing…");
[foo release];
NSLog(@"Released!");
if ([objh holdingObject]) {
NSLog(@"wait, what? you should not be seeing this line");
}
[objh release];
[pool drain];
return 0;
}
/*
2011-08-04 17:12:55.863 weaksauce[31567:1307] Releasing…
2011-08-04 17:12:55.866 weaksauce[31567:1307] FMFancyObject dealloc!
2011-08-04 17:12:55.867 weaksauce[31567:1307] Released!
2011-08-04 17:12:55.868 weaksauce[31567:1307] wait, what? you should not be seeing this line
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment