NSKeyedUnarchiver memory leak issue

爱⌒轻易说出口 提交于 2020-01-01 19:14:13

问题


I have problem with this code, it's working on debug environment. On the instruments I'm seeing memory leak problem on this function, instruments is giving warning that

Category Event Type Timestamp Address Size Responsible Library Responsible Caller 27 SocialNetwork Malloc 00:19.951 0x3d64d20 80 Foundation -[NSKeyedUnarchiver _decodeArrayOfObjectsForKey:]

- (NSMutableArray *)GetDataInstanceToUserDefaults{
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];   
NSData *storedObject = [userDefaults objectForKey:@"MyDataKey"];
NSMutableArray *data;   

if(storedObject != nil)
{       
    NSArray *savedArray = [NSKeyedUnarchiver unarchiveObjectWithData:storedObject];
    if(savedArray != nil)
        data = [[NSMutableArray alloc] initWithArray:savedArray];
    else
        data = [[NSMutableArray alloc] init];
}else{
    data = [[NSMutableArray alloc] init];   
}   
return data; 

}

I didn't understand where is the my problem ?

Thank you for your support

Edit : By the way I should give more detail about this problem,this function (as you can see) is storing my object. My object is custom class and storing in the NSMutableArray.

I already added these methods inside of the my custom class

-(void)encodeWithCoder:(NSCoder *)coder{
-(id)copyWithZone:(NSZone*)zone {
-(id)initWithCoder:(NSCoder *)coder{

回答1:


I think the problem is most likely in the initWithCoder: method of your custom class. It is leaking but the analyzer reports it as being in the archiver.

Unrelated to your problem, I would caution you against using [[NSMutableArray alloc] init] to initialize collections, especially mutable collections. Instead use, [[NSMutableArray alloc] initWithCapacity:1]. I've seen strange problems using just init that were cleared up by using initWithCapacity.



来源:https://stackoverflow.com/questions/1933285/nskeyedunarchiver-memory-leak-issue

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