NSString stringWithUTF8String中的内存泄漏:启用了ARC [英] Memory leak in NSString stringWithUTF8String: with ARC enabled

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

问题描述

在我的应用程序中,我启用了ARC.但是在我的应用程序中,根据仪器的不同,以下几行会给我造成内存泄漏.它是在ios 7.0中.

-(id)init{
    variables = [[NSMutableArray  alloc] init]; // Leak
    events = [[NSMutableArray  alloc] init]; //Leak
    return self;

}

更新

但是在我的应用程序中,如果我执行以下操作,则不会显示任何泄漏.但是我无法将项目添加到变量中.

-(id)init{
    variables = [[[NSMutableArray  alloc] init] copy]; // No Leak
    events = [[[NSMutableArray  alloc] init] copy]; //No Leak
    return self;

}

-

NSString *utfString =[NSString stringWithUTF8String:(const char *)attr->children->content];//Leak

-

-(NSObject*)createObjectForClass:(NSString*)className{
    Class cls = NSClassFromString(className);
    NSObject *object = [[cls alloc]init]; //Leak
    if(cls != nil){
        CFRelease((__bridge CFTypeRef)(cls));
    }
    return object;
}

有人知道如何解决此问题吗?

解决方案

我现在的猜测是整个对象都在泄漏,这意味着在-init中创建的NSMutableArray也会泄漏.调用copy的版本没有泄漏,因为该副本可能返回NSArray的单例实例(因为其中有零个元素,并且它是不可变的NSArray,因此可能有一个单例实例).

In my application i have enabled the ARC. But in my application following lines gives me memory leaks according to instruments. It is in ios 7.0.

-(id)init{
    variables = [[NSMutableArray  alloc] init]; // Leak
    events = [[NSMutableArray  alloc] init]; //Leak
    return self;

}

Update

But in my app if i do something like below it does not show me any leak. But i can't add items in to the variables.

-(id)init{
    variables = [[[NSMutableArray  alloc] init] copy]; // No Leak
    events = [[[NSMutableArray  alloc] init] copy]; //No Leak
    return self;

}

--

NSString *utfString =[NSString stringWithUTF8String:(const char *)attr->children->content];//Leak

--

-(NSObject*)createObjectForClass:(NSString*)className{
    Class cls = NSClassFromString(className);
    NSObject *object = [[cls alloc]init]; //Leak
    if(cls != nil){
        CFRelease((__bridge CFTypeRef)(cls));
    }
    return object;
}

Does anyone has any idea how to fix this?

解决方案

My guess right now is that your entire object is leaking, which means that the NSMutableArrays created in -init also leak. The version that calls copy isn't leaking because the copy is probably returning a singleton instance of NSArray (as there are zero elements in it, and it's an immutable NSArray, there's probably a singleton instance for that).

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

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