2012-07-17 49 views
-1

即使我使用autorelease釋放結果對象,泄漏工具也會在泄漏儀器中顯示100%泄漏。使用autorelease時泄漏?

ResultsViewController *results = [[[ResultsViewController alloc]initWithNibName: 
           @"ResultsViewController1" bundle:nil] autorelease]; 
[results getscore:(int)score:(int)skippedwords:(int)crtwords]; 
[self.navigationController pushViewController:results animated:YES];  
+0

我不知道,甚至會編譯。第二行是什麼?但是如果我們忽略這一點,你的代碼就很好。這意味着泄漏(如果有的話)在其他地方。 – 2012-07-17 12:17:20

+0

這段代碼沒問題。也許儀器顯示你泄漏,因爲你有一個在ResultsViewController – iSofTom 2012-07-17 12:22:03

+0

從第二行泄漏我將值傳遞給resultsviewcontroller – gangadhar 2012-07-17 14:17:12

回答

0

在「Instruments」它總是會顯示您爲「泄漏」

做一個件事分配/釋放「results」自己的自動釋放的對象。永遠不要使用「Autorelease」,除非你必須將對象返回給其他類或方法。

ResultsViewController *results = [[ResultsViewController alloc]initWithNibName: @"ResultsViewController1" bundle:nil]; 

[results getscore:score :skippedwords :crtwords]; 

[self.navigationController pushViewController:results animated:YES]; 

[results release]; 

因爲您已將此控制器推送到導航控制器,因此您不再需要它的實例。快樂編碼;)

+0

您將在此代碼中擁有Exc_Bad_Access,因爲您有兩個版本!實際上是錯誤的 – iSofTom 2012-07-17 12:20:58

+0

!我已經糾正了這個問題:) – 2012-07-17 12:22:12

+0

在問題中使用autorelease沒有任何問題。 Pre ARC有一個很好的論點,認爲這樣做比調用release更好,因爲它不太容易出現手動錯誤 – wattson12 2012-07-17 12:25:05