NSKeyedUnarchiver内存泄漏问题 [英] NSKeyedUnarchiver memory leak issue

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

问题描述

我有这个代码的问题,它的工作在调试环境。在仪器上我看到这个功能的内存泄漏问题,仪器会给警告

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

类别事件类型时间戳地址大小责任图书馆负责任的调用者
27 SocialNetwork Malloc 00:19.951 0x3d64d20 80基础 - [NSKeyedUnarchiver _decodeArrayOfObjectsForKey:]

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; 

}

在哪里是我的问题?

感谢您的支持

编辑:关于这个问题的详细信息,这个函数(如你所见)是存储我的对象。我的对象是自定义类并存储在NSMutableArray中。

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{


推荐答案

我认为问题很可能在 initWithCoder: 您的自定义类的方法。它泄漏,但分析仪报告它在归档器。

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.

与您的问题无关,我提醒您不要使用 [[NSMutableArray alloc] init] 来初始化集合,特别是可变集合。而应使用 [[NSMutableArray alloc] initWithCapacity:1] 。我已经看到使用 init 使用 initWithCapacity 清除了奇怪的问题。

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.

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

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