使用 NSKeyedUnarchiverdecodeObjectForKey 解码 NSArray 时出现内存泄漏

发布于 2024-11-19 18:45:25 字数 1280 浏览 3 评论 0原文

我正在尝试修复方法中某处导致的巨大内存泄漏,该方法从 CoreData 数据存储中获取 NSData 对象并将该 NSData 取消归档回 NSArray。

- (CustomObject *) getCustomObject
{
.... get myManagedObject from CoreData Store

    CustomObject *customObject = nil;
    if(myManagedObject)
    {
        customObject = [[[CustomObject alloc] init] autorelease];

        NSData *arrayData = [myManagedObject valueForKey:@"resultArray"];

        /* read and save resultArray */
        NSKeyedUnarchiver *unArchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:arrayData];

        /* per definition decodeObjectForKey returns an autoreleased object */
        /* Instruments (The Profiler) points out this line for the source */
        /* of the memory leak. */
        NSArray *myArray = [unArchiver decodeObjectForKey:@"rArray"];

        /* myArray is a (nonatomic,retain) property in CustomObject */
        [customObject setMyArray:myArray];

        /* finishDecoding and release unarchiver */
        [unArchiver finishDecoding];
        [unArchiver release];
    }

    return customObject;
}

如果我释放 myArray (我不应该这样做,因为它是一个自动释放的对象),内存泄漏就会消失。

有谁知道如何解决这个问题? CustomObject 的 NSArray 中的项目是 NSDictionary 对象。

提前致谢

编辑: - (CustomObject *) getCustomObject 被频繁调用,因此这次泄漏的影响相当大。

I'm trying to fix a huge memory leak that is caused somewhere in a method, that gets a NSData object from a CoreData datastore and unarchives that NSData back into a NSArray.

- (CustomObject *) getCustomObject
{
.... get myManagedObject from CoreData Store

    CustomObject *customObject = nil;
    if(myManagedObject)
    {
        customObject = [[[CustomObject alloc] init] autorelease];

        NSData *arrayData = [myManagedObject valueForKey:@"resultArray"];

        /* read and save resultArray */
        NSKeyedUnarchiver *unArchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:arrayData];

        /* per definition decodeObjectForKey returns an autoreleased object */
        /* Instruments (The Profiler) points out this line for the source */
        /* of the memory leak. */
        NSArray *myArray = [unArchiver decodeObjectForKey:@"rArray"];

        /* myArray is a (nonatomic,retain) property in CustomObject */
        [customObject setMyArray:myArray];

        /* finishDecoding and release unarchiver */
        [unArchiver finishDecoding];
        [unArchiver release];
    }

    return customObject;
}

If i release myArray (what i should not do, since it is a autoreleased object) the memory leak disappears.

Does anyone know how you can solve this issue? The items in the NSArray of the CustomObject are NSDictionary objects.

thanks in advance

edit: - (CustomObject *) getCustomObject is called very often, so the impact of this leak is quite big.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

以为你会在 2024-11-26 18:45:25

如果您从 Instruments 中找到此信息 - 它显示的是创建泄漏对象的位置,而不是泄漏对象的位置。

这意味着您将 myArray 传递给了一些没有正确释放它的东西。您是否在 customObjects dealloc 中正确释放了数组?

If you're finding this from Instruments - it's showing the place where the leaked object is created, not where it is leaked.

What this is saying is that you're passing myArray to something that is not releasing it properly further down the line. Are you releasing the array properly in you customObjects dealloc?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文