Plist 序列化造成的内存泄漏

发布于 2024-09-15 12:16:21 字数 592 浏览 12 评论 0原文

请帮我解决这个内存泄漏问题。在泄漏工具中,它显示了泄漏:库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 技术交流群。

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

发布评论

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

评论(5

绿光 2024-09-22 12:16:21

首先,确保您没有使用已弃用或过时的方法调用。根据您的应用程序配置(这由您决定),您可能正在使用过时的方法调用;来自苹果文档:

propertyListFromData:mutabilityOption:format:errorDescription:

此方法已过时,很快就会被弃用。 (已弃用。请改用 propertyListWithData:options:format:error:。)

在使用推荐的 api 调用后,我没有检测到内存泄漏...测试代码:

NSArray *somearray = @[@"One",@"Two",@"Three"];
NSData *rawCourseArray = [NSKeyedArchiver archivedDataWithRootObject:somearray];

NSPropertyListFormat format;
NSError *anError = nil;
id plist;
plist = [NSPropertyListSerialization propertyListWithData:rawCourseArray options:NSPropertyListImmutable format:&format error:&anError];
if (!plist){
    [anError release];
}
NSArray *entries = (NSArray *)plist;
for (NSDictionary *entry in entries)
{
    // DO SOMETHING
    NSLog(@"%@",entry);
}

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:

propertyListFromData:mutabilityOption:format:errorDescription:

This method is obsolete and will be deprecated soon. (Deprecated. Use propertyListWithData:options:format:error: instead.)

I did not detect a memory leak after using the recommended api call... Test Code:

NSArray *somearray = @[@"One",@"Two",@"Three"];
NSData *rawCourseArray = [NSKeyedArchiver archivedDataWithRootObject:somearray];

NSPropertyListFormat format;
NSError *anError = nil;
id plist;
plist = [NSPropertyListSerialization propertyListWithData:rawCourseArray options:NSPropertyListImmutable format:&format error:&anError];
if (!plist){
    [anError release];
}
NSArray *entries = (NSArray *)plist;
for (NSDictionary *entry in entries)
{
    // DO SOMETHING
    NSLog(@"%@",entry);
}
找回味觉 2024-09-22 12:16:21

语句 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.

淡写薰衣草的香 2024-09-22 12:16:21

尝试一下我们在 temp 中获取字典

    NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:plistPath];
    NSString *errorDesc = nil;
    NSPropertyListFormat format;
    NSDictionary *temp = (NSDictionary *)[NSPropertyListSerialization propertyListFromData:plistXML mutabilityOption:NSPropertyListMutableContainersAndLeaves format:&format errorDescription:&errorDesc];
    if (!temp)
   {
        NSLog(@"Error reading plist: %@, format: %d", errorDesc, format);
    }

Try this by we get dictionary in temp

    NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:plistPath];
    NSString *errorDesc = nil;
    NSPropertyListFormat format;
    NSDictionary *temp = (NSDictionary *)[NSPropertyListSerialization propertyListFromData:plistXML mutabilityOption:NSPropertyListMutableContainersAndLeaves format:&format errorDescription:&errorDesc];
    if (!temp)
   {
        NSLog(@"Error reading plist: %@, format: %d", errorDesc, format);
    }
白馒头 2024-09-22 12:16:21

没有泄漏。将其全部包装在 @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!

自在安然 2024-09-22 12:16:21

尝试以这种方式阅读你的 plist:

NSDictionary *dTmp=[[NSDictionary alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"data" ofType:@"plist"]];


self.myarray=[dTmp valueForKey:@"Objects"];

Try to read your plist in this manner:

NSDictionary *dTmp=[[NSDictionary alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"data" ofType:@"plist"]];


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