標籤:
1. assign意味著直接賦值,retain意味著release舊值,retain新值 1.1 @property (assign, nonatomic) UIWindow *window; 意味著:
- (void )setWindow:(UIWindow *)window
{
_window = window;
}
// AppDelegate.m
// 因為是assign,所以不會對[[UIWindow alloc]init]產生的對象做retain操作. 在自動釋放池釋放時,引用計數器會由1變成0,AppDelegate不能長期擁有該Window
self.window = [[[UIWindow alloc]init]autorelease];
//既然自動釋放池釋放時,會把產生的Window銷毀,不添加autorelease呢?這樣不符合記憶體管理原則,這麼做意味著,產生的Window永遠無法銷毀
self.window.backgroundColor = [UIColor redColor];
self.window.frame = [UIScreen mainScreen].bounds;
[self.window makeKeyAndVisible];
return YES;
// 如果用下面修改上面的代碼為下面這樣,貌似AppDelegate既可以長期擁有Window,AppDelegate釋放時,也可以銷毀Window
self.window = [[UIWindow alloc]init];
// 潛在問題是,如果對self.window重複賦值,例如再寫一行代碼 self.window = [[UIWindow alloc]init]. AppDelegate釋放時,就無法銷毀舊的Window
self.window.backgroundColor = [UIColor redColor];
self.window.frame = [UIScreen mainScreen].bounds;
[self.window makeKeyAndVisible];
return YES;
- (void)dealloc{
[self.window release];
[super dealloc];
}
1.2 @property (retain, nonatomic) UIWindow *window; 意味著:
- (void )setWindow:(UIWindow *)window
{
if (_window != window) {
[_window release];
_window = [window retain];
}
}
// AppDelegate.m
// 因為是retain,所以會對[[UIWindow alloc]init]產生的對象做retain,在自動釋放池釋放時,會由2變成1,AppDelegate可以長期擁有該Window
self.window = [[[UIWindow alloc]init]autorelease];
self.window.backgroundColor = [UIColor redColor];
self.window.frame = [UIScreen mainScreen].bounds;
[self.window makeKeyAndVisible];
return YES;
// 為了防止記憶體泄露,在AppDelegte銷毀的時候,要對擁有的window做release操作
- (void)dealloc{
[self.window release];
[super dealloc];
}
2.非ARC記憶體管理的由來 2.1 準備:
-------------Dog----------Begin-------------
#import <Foundation/Foundation.h>
@interface Dog : NSObject
@end
#import "Dog.h"
@implementation Dog
- (void)dealloc
{
NSLog(@"Dog--dealloc");
[super dealloc];
}
@end
-------------Dog----------End-------------
-------------Person----------Begin-------------
#import <Foundation/Foundation.h>
@class Dog;
@interface Person : NSObject
{
Dog *_dog;
}
- (void)setDog:(Dog *)dog;
- (Dog *)dog;
@end
#import "Person.h"
@implementation Person
- (void)setDog:(Dog *)dog
{
_dog = dog;
}
- (Dog *)dog
{
return _dog;
}
- (void)dealloc
{
NSLog(@"Person--dealloc");
[super dealloc];
}
@end
-------------Person----------End-------------
2.2 原始版本
-------------MainViewController----------Begin-------------
Dog *d = [[Dog alloc]init];
Person *p =[[Person alloc]init];
p.dog = d;
[d release];
NSLog(@"p.dog----%@",p.dog); //殭屍對象
[p release];
-------------MainViewController----------End-------------
2.3 最佳化,解決殭屍對象的問題
修改Person的Set方法
- (void)setDog:(Dog *)dog
{
_dog = [dog retain]; // 這樣MainViewController就不會訪問殭屍對象
}
- (void)dealloc
{
NSLog(@"Person--dealloc");
[self.dog release]; //誰retain,誰release
[super dealloc];
}
2.4 存在的問題:重複賦值時,Person銷毀時,只能釋放最新持有的Dog,舊Dog記憶體泄露
Dog *d = [[Dog alloc]init];
Dog *d2 = [[Dog alloc]init];
Person *p =[[Person alloc]init];
p.dog = d;
p.dog = d2;
[d release];
NSLog(@"p.dog----%@",p.dog);
[p release];
2.5 最佳化:解決重複賦值的問題
- (void)setDog:(Dog *)dog
{
[_dog release];
_dog = [dog retain];
}
2.6 存在的問題:重複賦值同一條狗,會出現野指標錯誤
Dog *d = [[Dog alloc]init];
Person *p =[[Person alloc]init];
p.dog = d;
p.dog = d; // 執行到這一行時,會出現野指標錯誤
[d release];
NSLog(@"p.dog----%@",p.dog);
[p release];
2.7 最佳化:在set方法加判斷
- (void)setDog:(Dog *)dog
{
if (_dog != dog) {
[_dog release];
_dog = [dog retain];
}
}
3. 屬性用retain相當於產生了2.6最佳化之後set/get方法;如果用assign,2.6最佳化之後的set方法還的寫上,這樣對象才能長期持有該屬性 3.1 修改person.h
@interface Person : NSObject
@property (retain,nonatomic) Dog *dog;
@end
3.2 修改person.m,dog的set方法和get可以去掉了4. 總結,做非ARC開發
1>要長期持有一個屬性用retain. 注:一般情況,屬性除了UI控制項,一般都是想長期持有的
2>在dealloc方法裡,要釋放:
寫法一:[self.屬性名稱 release];
_屬性名稱 = nil;
寫法二:[_屬性名稱 release];
_屬性名稱 = nil;
寫法三:[self.屬性名稱 nil];
5.
iOS MRC開發