NSString stringwithFormat ARC出现内存泄漏 [英] NSString stringwithFormat memory leak with ARC

查看:223
本文介绍了NSString stringwithFormat ARC出现内存泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理内存泄漏问题,但我无法弄清楚到底是什么问题(我在仪器方面也没有太多经验,因此,如果我要提出明显的要求,请原谅).

Hi I'm dealing with a memory leak but I cannot figure out what problem it is (I don't have either much experience with instruments so please excuse me if I'm asking something obvious).

基本上,我在类中有两个字符串作为属性,将在主队列中检索将显示给用户的第一个字符串,并在后台队列中检索不需要的字符串:

Basically I have two strings as properties in my class, the very first that will be shown to the user is retrieved in the main queue, and the one that is not required immediately is retrieved in a background queue:

@property (nonatomic, strong) NSString *stringDefaultLocationAddress;
@property (nonatomic, strong) NSString *stringCurrentLocationAddress;

-(void)viewDidLoad{
  ...
           dispatch_async(idQueue, ^(void) {
            [self recuperaDireccionActualEnBackground:currentUserLocation.coordinate];
         });

}

- (void)dealloc{
    [self removeObserver:self forKeyPath:@"playerProfileNeedsUpdate"];
    self.stringCurrentLocationAddress = nil;
    self.stringDefaultLocationAddress = nil;
}

但是我在仪器中遇到了这种泄漏:

But I'm getting this leak in instruments:

问题与stringWithFormat中的占位符@%@ ..."有关,因为如果我只在该位置放@"Test",则泄漏消失了,但是我不知道为什么泄漏此现象,并且我想了解它.

The problem has to do with the placeholders @" %@..." in stringWithFormat, because if I just put @"Test" at that point the leak is gone, but I don't know why is leaking this and I would like to understand it.

谢谢.

推荐答案

仪器会告诉您分配泄漏对象的位置,但可能不是泄漏对象的位置.您需要查看对象的保留和释放历史记录(单击对象地址旁边的圆圈中的向右箭头).您必须手动分析每个保留,并将每个保留与逻辑上对应的版本相关联,直到找到不平衡的保留为止.

Instruments tells you the location where a leaked object was allocated, but that may not be the place where it leaked. You need to look at the object's history of retains and releases (click the right-pointing arrow in the circle next to its address). You have to manually analyze each, correlating each retain with the logically corresponding release until you find an unbalanced retain.

如果您在整个代码中都使用ARC,我怀疑您滥用了__bridge_retainedCFBridgingRetain().或者,也许您已经对CFStringRef进行了正确的转换,但是之后却无法正确进行手动引用计数.

If you're using ARC throughout your code, I suspect you've misused __bridge_retained or CFBridgingRetain(). Or perhaps you've done a proper bridge cast to a CFStringRef but then have failed to properly do manual reference counting after that.

绝对可以使用静态分析器进行构建,并努力消除它引起的所有问题(或至少向自己确认它们是误报).

Definitely build with the static analyzer and work to eliminate all of the issues it raises (or at least confirm to yourself that they're false positives).

这篇关于NSString stringwithFormat ARC出现内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆