我在我的iOS应用程序上运行内存分配的探查器,我发现当前已创建了8MB的内存并仍然存在于我的应用程序中。显然有些不对劲。所以我钻了下来,这是我可以告诉你的形象:
知道为什么这是原因吗?这似乎是一个自动释放的对象,所以它不应该被释放而不是存在于内存中吗?
以下是我调用函数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 :(得分:1)
我认为函数parseTagsInComment可以使用一些委托方法调用,也可以从工作线程的执行路径调用(不在主线程上)。
所以,你在函数中的第一行应该创建一个自动释放池,在最后一行它应该破坏池。
-(void) parseTagsInComment
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
//Body of your function
[pool release];
}