2010-08-25 117 views
0

我在樂器中出現了內存泄漏......我之前已經排序了一些內容,但是這個已經讓我難倒了!如果你能幫助,我會非常感激......這是泄漏的方法......它需要一個數據字典,根據它創建一個新的並返回它。我已經評論了泄漏的線條,以及它泄漏的百分比(不知道這是什麼意思),我試圖通過選擇alloc/init/release而不是autorelease來清理一些東西,但是似乎沒有任何區別...爲什麼我有內存泄漏?我無法解決這個問題

- (PetalLayerView *)makePetalLayerWithData:(NSDictionary *)sectionData isZeroIndexed(BOOL)zeroIndexed 
{ 
    NSMutableSet *petalsData = [[NSMutableSet alloc] init];  // 7.2% 
    NSArray *sections = [sectionData objectForKey:@"sections"]; 

    NSNumber *startIndex, *endIndex; 
    NSDictionary *petalData; 
    for(int i=0; i<sections.count; i++) 
    { 
     startIndex = [sections objectAtIndex:i]; 

     if(i < sections.count - 1) 
      endIndex = [sections objectAtIndex:i+1]; 
     else 
      endIndex = [sections objectAtIndex:0]; 

     if(!zeroIndexed) 
     { 
      startIndex = [NSNumber numberWithInt:[startIndex intValue]-1]; // 10.2% 
      endIndex = [NSNumber numberWithInt:[endIndex intValue]-1]; // 10.5% 
     } 

     petalData = [[NSDictionary alloc] initWithObjectsAndKeys:startIndex, @"startIndex", endIndex, @"endIndex", nil]; // 64.4% 
     [petalsData addObject:petalData]; // 7.7% 
     [petalData release]; 
    } 

    int maxLength = MAX(self.frame.size.width, self.frame.size.height); 
    CGRect petalFrame = CGRectMake((self.frame.size.width - maxLength)/2, (self.frame.size.height - maxLength)/2, maxLength, maxLength); 
    PetalLayerView *petalLayerView = [[[PetalLayerView alloc] initWithFrame:petalFrame] autorelease]; 

    NSString *tagGroupName = [sectionData objectForKey:@"section_name"]; 

    WheelModel *wheelModel = [WheelModel sharedInstance]; 

    if([sectionData objectForKey:@"filtered"]) 
    { 
     petalLayerView.outlineColor = [wheelModel.tagColors objectForKey:tagGroupName]; 
    } 
    petalLayerView.petalColor = [wheelModel.petalColors objectForKey:tagGroupName]; 
    petalLayerView.petalsData = petalsData; 
    [petalsData release]; 

    return petalLayerView; 
} 

任何幫助非常感謝!謝謝!

:-Joe

+1

構建和分析是什麼意思?在這樣的方法中尋找潛在的泄漏通常是相當不錯的。 – theMikeSwan 2010-08-25 21:19:34

回答

1

後釋放了startIndex和endIndex你確定泄漏內存? petalsData被petalLayerView保留(大概),所以所有被分配的數據都不應該消失(即它不是泄漏,它是有意分配的)。我認爲,視圖本身可能被泄露,但這會超出所提供代碼的範圍。

+0

謝謝......當你寫這個答案時,我發現了這一點。泄漏根本不存在,但在PetalLayerView中(如您所建議的那樣)。 PetalLayerView沒有在dealloc中釋放任何ivars ...我很愚蠢,我沒有意識到我必須跟隨泄漏到連接的對象。 感謝您的幫助:) – jowie 2010-08-25 21:25:15

-1

你只需要petalData = [[NSDictionary alloc] ...

+0

不,新創建的是'autoreleased',而從數組獲得的則不是。見例如[對象所有權策略](http://developer.apple.com/mac/library/documentation/cocoa/conceptual/MemoryMgmt/Articles/mmObjectOwnership.html#//apple_ref/doc/uid/20000043-SW1)。 – 2010-08-25 22:33:27

相關問題