2011-01-28 102 views
1

我的代碼有問題。我正在使用性能工具運行我的應用程序,以便我可以看到我的代碼使用了多少內存。當我使用UINavigationController的popViewController方法時,分配的位置似乎不會被釋放,即使我已經使用「self.myarray = nil;」釋放了viewDidUnload方法中的所有對象。 「myarray」在執行文件中被合成。造成這種情況的原因是什麼?iPhone內存管理

當顯示一個alertview時,它會增加分配的內存。沒關係。 但即使我點擊確定按鈕,它不釋放分配的內存。

示例代碼用於AlertView部件。

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Seri Seçilmedi"  
               message:@"Lütfen bir seri seçiniz." 
               delegate:self 
             cancelButtonTitle:@"Tamam" 
             otherButtonTitles:nil]; 

[alert show]; 
[alert release]; 

回答

1

viewDidUnload將在內存警告時調用,但不會與popViewController一起調用。您應該釋放dealloc方法中的對象。

-1

這是釋放分配給UIAlertViews的內存的最佳方式。就我使用它們而言,我已經在dealloc()中釋放它,如phix23所述。

if(self.alert.visible) { 
      [self.alert dismissWithClickedButtonIndex:-1 animated:NO]; 
     } 
    [alert release]; 
    self.alert = nil; 
+0

@Vijay有錯在你的代碼,釋放一個對象後,你應該將其設置爲nil 。但是在設置self.alert nil(不再存在)之後你會發布。 – 2011-08-05 17:22:19

+0

@ Control-V:對,我編輯了我的答案。順便說一句,我的名字是維拉,而不是維傑。 – Viraj 2011-08-06 14:01:07

-1
if(self.alert.visible) { 
      [self.alert dismissWithClickedButtonIndex:-1 animated:NO]; 
} 

[alert release];//If you only do [alert release], you will release the memory, but the alert still point to the memory 
self.alert = nil; // alert no longer exists. 

如果你只是釋放一個對象,那麼它會成爲釋放的對象。

如果您對釋放的對象執行任何操作,那麼您的應用程序會崩潰。爲避免它總是首選「釋放後將對象指定爲零」。因爲我們都知道,在零執行的任何操作都將不被執行:)

,或者你可以設置自動釋放

UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"Seri Seçilmedi"  
              message:@"Lütfen bir seri seçiniz." 
              delegate:self 
            cancelButtonTitle:@"Tamam" 
            otherButtonTitles:nil]autorelease];