2009-10-04 35 views
0

這裏是我的代碼:iPhone內存管理問題和對象的LiveCycle

電話SDK undestanding可可對象使用生命週期:

- (void) DismissWelcomeMessage: (UIAlertView *) view 
{ 
    [view dismissWithClickedButtonIndex:0 animated:YES]; 
} 

- (void) ShowWelcomeMessage 
{ 
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Blah" message:@"Blah Blah" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
    [alert show]; 
    [self performSelector:@selector (DismissWelcomeMessage:) withObject: alert afterDelay: WELCOME_MESSAGE_DELAY]; 

    [alert release]; 
} 

ShowWelcomeMessage首先調用。

爲什麼DissmissWelcomeMessage工作正常,即使發佈警報對象也不會崩潰?

是因爲Dismiss函數使用堆棧上傳遞的對象的副本作爲函數時的參數嗎?但即使如此,它不會只是指向現在釋放的對象的指針的副本?

或[警報發佈]只是引用計數,並沒有真正做到 在C++中刪除?

回答

3

performSelector保留該對象,因此您的發佈不會導致其保留計數爲零。

參見NSObject docs

這種方法保留了接收器和anArgument參數進行選擇之後,直至。

+0

謝謝,我錯過了NSObject文檔中的那部分內容! – leon 2009-10-04 03:49:38

0

performSelector可能會保留傳入的對象,這就是爲什麼當DismissWelcomeMessage被調用時它仍然有效。