Skip to content

Instantly share code, notes, and snippets.

@omochi
Created April 26, 2013 21:00
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 omochi/5470411 to your computer and use it in GitHub Desktop.
Save omochi/5470411 to your computer and use it in GitHub Desktop.
ARCとMRCを混ぜながらプログラムを書くための方法とその調査報告 ref: http://qiita.com/omochi@github/items/f27022d31e9914af30b8
How do I think about ARC? Where does it put the retains/releases?
Try to stop thinking about where the retain/release calls are put and think about your application algorithms instead. Think about “strong and weak” pointers in your objects, about object ownership, and about possible retain cycles.
-(BOOL)doSomethingWithError:(NSError *__autoreleasing *)error{
for(int i=0;i<10000;i++){
@autoreleasepool{
if(![self doSmallTask]){
if(error) *error = [self makeError];
return NO;
}
}
}
return YES;
}
NSLog(@"--- 5");
@autoreleasepool {
if(YES){
AMMARCClass * __strong f = [AMMARCClass makeRetainedWithName:@"fish"];
AMMARCClass * __strong g = [AMMARCClass makeRetainedAnnotatedWithName:@"girl"];
(void)f,(void)g;
}
NSLog(@"--- 6");
}
NSLog(@"--- 7");
--- 5
ARC fish init
ARC girl init
ARC girl dealloc
--- 6
ARC fish dealloc
--- 7
NSLog(@"--- 8");
@autoreleasepool {
if(YES){
AMMARCClass * h = [[[AMMARCClass alloc]initWithName:@"house"]autorelease];
AMMARCClass * i = [AMMARCClass makeRetainedWithName:@"ion"];
AMMARCClass * j = [AMMARCClass makeRetainedAnnotatedWithName:@"joke"];
(void)h,(void)i,(void)j;
[j release];
}
NSLog(@"--- 9");
}
NSLog(@"--- 10");
--- 8
ARC house init
ARC ion init
ARC joke init
ARC joke dealloc
--- 9
ARC ion dealloc
ARC house dealloc
--- 10
NSLog(@"--- 11");
@autoreleasepool {
if(YES) {
(void)[[AMMARCClass alloc]initWithName:@"knight"];
[AMMARCClass makeRetainedWithName:@"lime"];
[AMMARCClass makeRetainedAnnotatedWithName:@"money"];
(void)[[AMMMRCClass alloc]initWithName:@"nuke"];
[AMMMRCClass makeRetainedWithName:@"ocean"];
[AMMMRCClass makeRetainedAnnotatedWithName:@"pine"];
}
NSLog(@"--- 12");
}
NSLog(@"--- 13");
--- 11
ARC knight init
ARC knight dealloc
ARC lime init
ARC money init
ARC money dealloc
MRC nuke init
MRC nuke dealloc
MRC ocean init
MRC pine init
MRC pine dealloc
--- 12
ARC lime dealloc
--- 13
@implementation AMMARCClass
-(id)initWithName:(NSString *)name{
self = [super init];
if(self){
NSLog(@"ARC %@ init",name);
_name = name;
}
return self;
}
- (void)dealloc{
NSLog(@"ARC %@ dealloc",self.name);
}
@end
@implementation AMMMRCClass
-(id)initWithName:(NSString *)name{
self = [super init];
if(self){
NSLog(@"MRC %@ init",name);
_name = [name retain];
}
return self;
}
- (void)dealloc
{
NSLog(@"MRC %@ dealloc",self.name);
[_name release];
[super dealloc];
}
NSLog(@"--- 1");
@autoreleasepool {
if(YES){
AMMARCClass * __strong a = [[AMMARCClass alloc]initWithName:@"apple"];
AMMARCClass * __autoreleasing b = [[AMMARCClass alloc]initWithName:@"banana"];
(void)a,(void)b;
}
NSLog(@"--- 2");
}
NSLog(@"--- 3");
--- 1
ARC apple init
ARC banana init
ARC apple dealloc
--- 2
ARC banana dealloc
--- 3
@interface AMMMRCClass : NSObject
+(AMMMRCClass *)makeRetainedWithName:(NSString *)name;
+(AMMMRCClass *)makeRetainedAnnotatedWithName:(NSString *)name NS_RETURNS_RETAINED;
@end
@implementation AMMMRCClass
+(AMMMRCClass *)makeRetainedWithName:(NSString *)name{
return [[AMMMRCClass alloc]initWithName:name];
}
+(AMMMRCClass *)makeRetainedAnnotatedWithName:(NSString *)name{
return [[AMMMRCClass alloc]initWithName:name];
}
@end
NSLog(@"--- 3");
@autoreleasepool {
if(YES){
AMMMRCClass * __strong c = [[AMMMRCClass alloc]initWithName:@"cherry"];
AMMMRCClass * __strong d = [AMMMRCClass makeRetainedWithName:@"doll"];
AMMMRCClass * __strong e = [AMMMRCClass makeRetainedAnnotatedWithName:@"eye"];
(void)c,(void)d,(void)e;
}
NSLog(@"--- 4");
}
NSLog(@"--- 5");
--- 3
MRC cherry init
MRC doll init
MRC eye init
MRC eye dealloc
MRC cherry dealloc
--- 4
--- 5
@interface AMMARCClass : NSObject
+(AMMARCClass *)makeRetainedWithName:(NSString *)name;
+(AMMARCClass *)makeRetainedAnnotatedWithName:(NSString *)name NS_RETURNS_RETAINED;
@end
@implementation AMMARCClass
+(AMMMRCClass *)makeRetainedWithName:(NSString *)name{
return [[AMMMRCClass alloc]initWithName:name];
}
+(AMMMRCClass *)makeRetainedAnnotatedWithName:(NSString *)name{
return [[AMMMRCClass alloc]initWithName:name];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment