2011-06-21 24 views
-2

enter image description here 我得到那個多維可變數組我需要刪除這些泄漏memoery泄漏,因爲由於這種泄漏的應用程序不能在iPhone的工作,我DNT如何消除這種泄漏內存泄漏使用多維數組

的CGRect bounds = [自我界限];

UITouch*   touch = [[event touchesForView:self] anyObject]; 
if (firstTouch) { 
    firstTouch = NO; 
    previousLocation = [touch previousLocationInView:self]; 
    previousLocation.y = bounds.size.height - previousLocation.y; 
    NSMutableArray *temp=[[NSMutableArray alloc]init]; 
    [undo addObject:temp]; 
    /***** add 1st point *********/ 
    [[undo objectAtIndex:[undo count] -1] addObject:[NSValue valueWithCGPoint:previousLocation]]; 

    if(mcount==1) 
    { 
     [masking addObject:[[NSMutableArray alloc]init]]; 
     [[masking objectAtIndex:[masking count]-1] addObject:[NSValue valueWithCGPoint:previousLocation]]; 
    } 
} else { 

    location = [touch locationInView:self]; 
    location.y = bounds.size.height - location.y; 
    previousLocation = [touch previousLocationInView:self]; 
    previousLocation.y = bounds.size.height - previousLocation.y; 


    [[undo objectAtIndex:[undo count] -1]addObject:[NSValue valueWithCGPoint:previousLocation]]; 

    if(mcount==1) 
    { 
     [[masking objectAtIndex:[masking count]-1] addObject:[NSValue valueWithCGPoint:previousLocation]]; 
    } 
} 
+3

我沒有看到這裏問了一個問題。你的問題是什麼?你有什麼嘗試?你是否縮小了它?請編輯您的問題以添加此信息。 –

回答

2

行:

NSMutableArray *temp=[[NSMutableArray alloc]init]; 

似乎泄漏。當你完成它時發佈temp或在這一行中發佈autorelease

這條線:

[masking addObject:[[NSMutableArray alloc]init]]; 

也存在問題。既然沒有直接的指針,我推薦autorelease(或者重寫這段代碼,但我離題了)。

而且你可能有幫助的是,你可以改變

[undo objectAtIndex:[undo count] -1] 

,而不是成爲

[undo lastObjectAtIndex] 
+1

帶'addObject:'的行可以通過'[masking addObject:[NSMutableArray array]]'' – Eimantas

+0

'等工廠方法更改爲自動釋放數組,您不應該在循環中使用autorelease對象。使用 - [newArray mutableCopy];相反 - 在進入循環之前創建NSMutableArray,然後在使用數組之前 - [removeAllObjects];從它,然後添加任何對象(或保持原樣),並通過 - [masking addObject:[temp mutableCopy]]添加到父集合; –