2012-07-24 18 views
1

我正在內存分配上運行我的iOS應用程序分析器,並且檢測到當前已創建8MB內存,並且仍然存在於我的應用程序中。顯然有一些錯誤。所以,我鑽下來,這裏的形象,我可以告訴你:從儀器讀取內存分配結果

enter image description here

知道爲什麼這是什麼原因?這似乎是一個自動發佈的對象,所以不應該被釋放,而不是活在內存中?

以下是我正在調用該函數parseTagsInComment:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{ 
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; 

    NSString *commentsText = [NSString stringWithFormat:@"%@ %@", self.imageComment_.username_, self.imageComment_.text_]; 

    NSRange range; 
    range.location = 0; 
    range.length = commentsText.length; 

    NSMutableAttributedString* attrStr = [[NSMutableAttributedString alloc] initWithString:commentsText]; 
    [attrStr setFont:[UIFont fontWithName:@"HelveticaNeue" size:14] range:range]; 
    self.commentAttributedString_ = attrStr; 
    [attrStr release]; 


    dispatch_async(dispatch_get_main_queue(), ^{ 
     [weakSelf.commentsText_ setAlpha:0.0]; 
     [weakSelf.commentsPostedTime_ setAlpha:0.0]; 
     [weakSelf.commentsText_ setFrameWidth:weakSelf.contentView.frameWidth - weakSelf.profilePicture_.frameWidth - kCommentsPadding]; 
     [weakSelf.commentsText_ setFrameHeight:weakSelf.imageComment_.commentHeight_ - 30]; 
     [weakSelf.commentsText_ setAttributedString:weakSelf.commentAttributedString_]; 
     [weakSelf.commentsText_ setLinkColor:weakSelf.textColor_]; 

     NSString *timePosted = [NSString timestampToString:weakSelf.imageComment_.createdTime_]; 
     CGSize commentsTimeSize = [timePosted sizeWithFont:weakSelf.commentsPostedTime_.font constrainedToSize:CGSizeMake(weakSelf.commentsText_.frameWidth, 50)]; 
     [weakSelf.commentsPostedTime_ setText:timePosted]; 


     [UIView animateWithDuration:0.3 animations:^{ 
      [weakSelf.commentsText_ setAlpha:1.0]; 
      [weakSelf.commentsPostedTime_ setAlpha:1.0]; 
     } completion:^(BOOL finished){ 
      [weakSelf parseTagsInComment]; 
     }]; 
    }); 
    [pool release]; 
}); 
+0

您確定這些百分比不代表每個代碼行所需的*時間量嗎? (即這是內存數據而不是分析數據)? – Turix 2012-07-24 05:26:12

+0

@Turix我怎麼知道?我很確定它是內存..因爲我在Instruments上運行了分配測試而不是時間分析器 – adit 2012-07-24 05:26:36

+0

是的,分配(或泄漏)應該是你想要的,你是對的。 (雖然我問我的問題的原因是因爲屏幕截圖中的百分比對我來說看起來似乎更合理)。順便說一句,這看起來不像我從xcode運行它時從Allocations或Profile獲得的輸出,所以很難說。 – Turix 2012-07-24 05:32:30

回答

1

我覺得功能parseTagsInComment或者是所謂的一些代表法或工作者線程(而不是在主線程)的執行路徑。

所以,你函數中的第一行應該創建一個自動釋放池,在最後一行它應該銷燬該池。

-(void) parseTagsInComment 
{ 
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; 
    //Body of your function 
    [pool release]; 
} 
+0

實際上它是在dispatch_async上調用的..我在 – adit 2012-07-24 05:57:36

+0

之上添加了一些代碼@adit您是否嘗試過他建議的內容? (我認爲值得一試 - 這對我來說很有意義,因爲在dispatch_async上自動釋放的內存可能不會在主池的下一次刷新之前釋放,這在您的Allocations測試中可能還沒有得到。) – Turix 2012-07-24 06:06:50

+0

@Turix是的,我認爲它似乎解決了我的問題..但是,我可以得到關於這個自動發佈dispatch_async更詳細的解釋可能不會被釋放,直到下一次刷新郵件池..我也需要了解這一點而不只是使用解決方案 – adit 2012-07-24 06:08:32