Created
August 28, 2009 21:32
-
-
Save tjw/177270 to your computer and use it in GitHub Desktop.
This file contains 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
#import <Foundation/Foundation.h> | |
#import <objc/runtime.h> | |
#import <objc/objc-auto.h> | |
/* | |
cc -Wall -fobjc-gc objc-assoc.m -o objc-assoc -framework Foundation | |
*/ | |
@interface ExtraData : NSObject | |
@end | |
@implementation ExtraData | |
- (void)dealloc; | |
{ | |
fprintf(stderr, "ExtraData %p deallocated\n", self); | |
[super dealloc]; | |
} | |
- (void)finalize; | |
{ | |
fprintf(stderr, "ExtraData %p finalized\n", self); | |
[super finalize]; | |
} | |
@end | |
int main(int argc, char *argv[]) | |
{ | |
NSObject *owner = [[NSObject alloc] init]; | |
ExtraData *data = [[ExtraData alloc] init]; | |
static void *ExtraDataKey; | |
objc_setAssociatedObject(owner, &ExtraDataKey, data, OBJC_ASSOCIATION_RETAIN); | |
[data release]; | |
fprintf(stderr, "Releasing owner...\n"); | |
[owner release]; | |
// In GC-land, make sure we don't have any pointers to the owner | |
if ([NSGarbageCollector defaultCollector]) { | |
owner = nil; | |
data = nil; | |
objc_clear_stack(0); | |
[[NSGarbageCollector defaultCollector] collectExhaustively]; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment