Skip to content

Instantly share code, notes, and snippets.

@hiroshi
Created April 27, 2012 02:50
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 hiroshi/2505291 to your computer and use it in GitHub Desktop.
Save hiroshi/2505291 to your computer and use it in GitHub Desktop.
Associative Object は自動で release, property は自分で release ref: http://qiita.com/items/c262c9b9f8e1044ac2e4
$ clang main.m -o main -framework Cocoa && ./main
-[Container dealloc]
-[Content dealloc] property
-[Content dealloc] associatedObject
/* 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