使用 NSKeyedUnarchiverdecodeObjectForKey 解码 NSArray 时出现内存泄漏
我正在尝试修复方法中某处导致的巨大内存泄漏,该方法从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您从 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?