Created
April 27, 2012 02:50
-
-
Save hiroshi/2505291 to your computer and use it in GitHub Desktop.
Associative Object は自動で release, property は自分で release ref: http://qiita.com/items/c262c9b9f8e1044ac2e4
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
$ clang main.m -o main -framework Cocoa && ./main | |
-[Container dealloc] | |
-[Content dealloc] property | |
-[Content dealloc] associatedObject |
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
/* clang main.m -o main -framework Cocoa && ./main */ | |
#include <stdio.h> | |
#import <Cocoa/Cocoa.h> | |
#import <objc/runtime.h> | |
#define TRACE(msg) printf("%s %s\n", __PRETTY_FUNCTION__, msg) | |
@interface Content : NSObject | |
@property (nonatomic, retain) NSString *msg; | |
@end | |
@implementation Content | |
@synthesize msg=_msg; | |
- (id)initWithMessage:(NSString *)msg | |
{ | |
self = [super init]; | |
if (self) { | |
self.msg = msg; | |
} | |
return self; | |
} | |
- (void)dealloc | |
{ | |
TRACE(self.msg.UTF8String); | |
self.msg = nil; | |
[super dealloc]; | |
} | |
@end | |
@interface Container : NSObject | |
@property (nonatomic, retain) Content *content; | |
@end | |
static char associativeKey; | |
@implementation Container | |
@synthesize content=_content; | |
- (id)init | |
{ | |
self = [super init]; | |
if (self) { | |
id associatedObject = [[[Content alloc] initWithMessage:@"associatedObject"] autorelease]; | |
objc_setAssociatedObject(self, &associativeKey, associatedObject, OBJC_ASSOCIATION_RETAIN_NONATOMIC); | |
self.content = [[[Content alloc] initWithMessage:@"property"] autorelease]; | |
} | |
return self; | |
} | |
- (void)dealloc | |
{ | |
TRACE(""); | |
self.content = nil; | |
[super dealloc]; | |
} | |
@end | |
int main(int argc, char **argv) | |
{ | |
@autoreleasepool { | |
[[Container new] autorelease]; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment