Plist 序列化造成的内存泄漏
请帮我解决这个内存泄漏问题。在泄漏工具中,它显示了泄漏:库Foundation
中的NSCFString(32字节)负责框架:NSPropertyListSerialization
。我正在释放错误,但仍然存在泄漏。我缺少什么?非常感谢!
NSPropertyListFormat format;
NSString *anError = nil;
id plist;
plist = [NSPropertyListSerialization propertyListFromData:rawCourseArray mutabilityOption:NSPropertyListImmutable format:&format errorDescription:&anError];
if (!plist){
[anError release];
}
NSArray *entries = (NSArray *)plist;
for (NSDictionary *entry in entries)
{
// DO SOMETHING
}
Please help me with this memory leak. In the leaks tool it shows a leak: NSCFString (32 bytes) in the library Foundation
Responsible Frame: NSPropertyListSerialization
. I am releasing the error but still a leak. What am I missing? Many thanks!
NSPropertyListFormat format;
NSString *anError = nil;
id plist;
plist = [NSPropertyListSerialization propertyListFromData:rawCourseArray mutabilityOption:NSPropertyListImmutable format:&format errorDescription:&anError];
if (!plist){
[anError release];
}
NSArray *entries = (NSArray *)plist;
for (NSDictionary *entry in entries)
{
// DO SOMETHING
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
首先,确保您没有使用已弃用或过时的方法调用。根据您的应用程序配置(这由您决定),您可能正在使用过时的方法调用;来自苹果文档:
在使用推荐的 api 调用后,我没有检测到内存泄漏...测试代码:
First, make sure that you are not using deprecated or obsolete method calls. Depending on your app configuration (this is for you to decide) you may be using obsolete method calls; from Apple docs:
I did not detect a memory leak after using the recommended api call... Test Code:
语句 plist = [NSPropertyListSerialization propertyListFromData:rawCourseArray mutabilityOption:NSPropertyListImmutable format:&format errorDescription:&anError];
创建一个自动释放对象。如果您的代码现在在一个单独的线程中运行,而
@autoreleasepool {...}
未向该线程显式分配自动释放池,则该对象永远无法被释放,并且将发生泄漏。因此,如果您的代码在单独的线程中运行,请确保您已设置自动释放池。
The statement
plist = [NSPropertyListSerialization propertyListFromData:rawCourseArray mutabilityOption:NSPropertyListImmutable format:&format errorDescription:&anError];
creates an autorelease object. If your code now runs in a separate thread to which no autorelease pool has been assigned explicitly by
@autoreleasepool {...}
, this object can never been released and will be a leak.So, please ensure that you have set up an autorelease pool if your code runs in a separate thread.
尝试一下我们在 temp 中获取字典
Try this by we get dictionary in temp
没有泄漏。将其全部包装在 @autoreleasepool 中,以确保自动释放的所有内容都立即作为测试消失。
然后消除由 anError 的双重释放引起的潜在崩溃:
它是自动释放的,您不必再次释放它!
there is no leak. wrap it all in an @autoreleasepool to be sure everything that is autoreleased goes away right away as a test.
THEN get rid of that potential crash caused by the double free of anError:
it is autoreleased and you don't have to release it again!
尝试以这种方式阅读你的 plist:
Try to read your plist in this manner: