2012-08-17 230 views
0

這是我的第一篇文章,所以我希望格式化可以。內存泄漏:在線分配的對象的潛在泄漏

我得到這個泄漏「上線的對象的潛在泄漏」,我無法弄清楚。也許我太看重它,因此我看不到問題。有人可以幫我嗎?

該生產線的問題是:appDelegate.imageText

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

    PListFirstAppDelegate *appDelegate = (PListFirstAppDelegate *)[[UIApplication sharedApplication]delegate]; 

    appDelegate.imageText= [[NSString alloc]initWithFormat:@"%@",[[array objectAtIndex:indexPath.row]objectForKey:@"image"]]; 

    //NSLog(@"%@", appDelegate.imageText); 

    NavigationalDescription *detailViewController = [[NavigationalDescription alloc] initWithNibName:@"NavigationalDescription" bundle:nil]; 


    // Pass the selected object to the new view controller. 

    [self.navigationController pushViewController:detailViewController animated:YES]; 
    [detailViewController release]; 

} 

回答

0

你應該在該行的末尾添加一個自動調用。 的iPad不會讓我複製粘貼代碼:(所以現在沒有一例。

appDelegate.imageText= [[[NSString alloc]initWithFormat:@"%@",[[array objectAtIndex:indexPath.row]objectForKey:@"image"] ] autorelease]; 

你的意思是委託在內存管理的imagetext實例的控制。他們應該保留字符串作爲他們需要它。

+0

感謝這個工作替換代碼。 – user1309075 2012-10-08 16:56:27

+0

很高興它的工作,你應該接受一個答案供他人以後參考。 – rooftop 2012-10-08 18:17:01

0
appDelegate.imageText= [[NSString alloc]initWithFormat:@"%@",[[array objectAtIndex:indexPath.row]objectForKey:@"image"]]; 

要指定一個分配的字符串圖像文本財產。財產分配應該是一個自動釋放的字符串。i​​mageText應該是保留財產,因此保留字符串的falue被分配給它。如果它保留,你是雙重保留,因此泄漏物體。

1

每次使用任何init方法實例化對象時,都必須在某處釋放它,或者調用autorelease。

類方法返回的東西應該總是自動釋放。

因此,而不是使用[[NSString alloc] initWithFormat...]

[NSString stringWithFormat:@"%@", myString]代替。

1

appDelegate.imageText= [[NSString alloc]initWithFormat:@"%@",[[array objectAtIndex:indexPath.row]objectForKey:@"image"]]; 

appDelegate.imageText= [NSString stringWithFormat:@"%@",[[array objectAtIndex:indexPath.row] objectForKey:@"image"]]; 
+3

如果您正在開發新項目,那麼如果您可以開始使用ARC,那將會很棒。 – 2012-08-17 17:54:48