你如何消除仪器中发现的泄漏

时间:2012-07-24 05:48:42

标签: iphone objective-c ios ipad nimbus-ios

我刚刚在Instruments中运行Leaks工具并发现了以下泄漏:

enter image description here

基本上它指向了NIAttributedLabel中的drawRect,所以我双击了drawRect方法,这就是我所拥有的:

我如何消除这种泄漏? enter image description here

以下是一些代码:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{

            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];
                [weakSelf.commentsPostedTime_ setFrameWidth:commentsTimeSize.width];
                [weakSelf.commentsPostedTime_ setFrameHeight:commentsTimeSize.height];
                [weakSelf.commentsPostedTime_ setFrameY:weakSelf.commentsText_.frameY + weakSelf.commentsText_.frameHeight];
                [weakSelf.commentsPostedTime_ setFrameX:weakSelf.commentsText_.frameX];

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

        });

1 个答案:

答案 0 :(得分:0)

如果您在最新的主设备上使用Nimbus,那么您必须启用ARC,否则会出现内存泄漏。