這是處理記憶的正確方法嗎?Relaese,Alloc和Nil的一個對象?
方法1:沒有崩潰
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(220.0f, 7.0f, 57.0f, 35.0f)] ;
button = nil;
[button release];
方法2:用崩潰
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(220.0f, 7.0f, 57.0f, 35.0f)] ;
[button release];
button = nil;
將變量設置爲零是很好的做法,因爲當你不這樣做時,如果稍後向變量發送消息,則會因爲嘗試將消息發送到釋放的實例而發生崩潰。如果將其設置爲零,則只會得到零結果,程序將繼續運行。嘗試'NSMutableString * str = [[NSMutableString alloc] initWithString:@「bla」]; NSLog(@「rc =%d」,[str retainCount]); [str release]; str = nil; NSLog(@「rc =%d」,[str retainCount]);' - 這會運行。取消'str = nil',你會發生崩潰。 – fzwo 2011-05-30 08:15:40