Memory leak in NSString stringWithUTF8String: with ARC enabled

◇◆丶佛笑我妖孽 提交于 2019-12-18 09:37:05

问题


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?


回答1:


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).



来源:https://stackoverflow.com/questions/21423309/memory-leak-in-nsstring-stringwithutf8string-with-arc-enabled

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!