Created
April 26, 2013 21:00
-
-
Save omochi/5470411 to your computer and use it in GitHub Desktop.
ARCとMRCを混ぜながらプログラムを書くための方法とその調査報告 ref: http://qiita.com/omochi@github/items/f27022d31e9914af30b8
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
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. |
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
-(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; | |
} |
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
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"); |
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
--- 5 | |
ARC fish init | |
ARC girl init | |
ARC girl dealloc | |
--- 6 | |
ARC fish dealloc | |
--- 7 |
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
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"); |
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
--- 8 | |
ARC house init | |
ARC ion init | |
ARC joke init | |
ARC joke dealloc | |
--- 9 | |
ARC ion dealloc | |
ARC house dealloc | |
--- 10 |
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
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"); |
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
--- 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 |
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
@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 |
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
@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]; | |
} |
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
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"); |
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
--- 1 | |
ARC apple init | |
ARC banana init | |
ARC apple dealloc | |
--- 2 | |
ARC banana dealloc | |
--- 3 |
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
@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 |
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
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"); |
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
--- 3 | |
MRC cherry init | |
MRC doll init | |
MRC eye init | |
MRC eye dealloc | |
MRC cherry dealloc | |
--- 4 | |
--- 5 |
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
@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