As far as I know str1 is released after assignment. I just receive warning but why don't it crash?
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
UIViewController * vc = [[UIViewController alloc] init] ;
self.window.rootViewController = vc ;
[self.window makeKeyAndVisible];
NSString * __unsafe_unretained str1 = [[NSString alloc] initWithFormat:@"First Name: %@", @"cc"] ;
// I think it should crash here !
NSLog(@"string: %u", [str1 length]) ;
NSString * __weak str2 = [[NSString alloc] initWithFormat:@"First Name: %@", @"cc"] ;
// nil for str2
NSLog(@"string: %@", str2) ;
return YES;
}
It may be clear if I modify the code like this.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
UIViewController * vc = [[UIViewController alloc] init] ;
self.window.rootViewController = vc ;
[self.window makeKeyAndVisible];
NSString * __unsafe_unretained str1 = [[NSString alloc] initWithFormat:@"First Name: %@", @"cc"] ;
// I think it should crash here !
NSLog(@"string: %u", [str1 length]) ;
TestObject * __unsafe_unretained obj1 = [[TestObject alloc] init] ;
NSLog(@"obj1:%@", obj1) ;
return YES;
}
TestObject.m:
@implementation TestObject
- (void)dealloc
{
NSLog(@"%@, %@", NSStringFromSelector(_cmd), self) ;
}
@end
The log is
2013-12-09 15:49:08.625 xxx[23950:a0b] string: 14
2013-12-09 15:49:08.627 xxx[23950:a0b] dealloc, <TestObject: 0x8970ff0>
2013-12-09 15:49:08.627 xxx[23950:a0b] obj1:<TestObject: 0x8970ff0>