Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Memory Leak with Plist Serialization

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
    }
like image 372
BX69 Avatar asked Aug 23 '10 00:08

BX69


People also ask

How do you identify a memory leak?

To find a memory leak, look at how much RAM the system is using. The Resource Monitor in Windows can be used to accomplish this. In Windows 8.1 and Windows 10: To open the Run dialogue, press Windows+R, then type "resmon" and click OK.

Can memory leaks happen on stack?

Stack memory leaks occur when a method keeps getting called but never exits. This can happen if there is an infinite loop or if the method is being called with different data each time but the data is never used. Eventually, the stack will fill up and the program will run out of memory.

What is a memory leak and how would you avoid it?

Memory leak occurs when programmers create a memory in heap and forget to delete it. The consequences of memory leak is that it reduces the performance of the computer by reducing the amount of available memory.

What is memory leak with example?

The memory leak would occur if the floor number requested is the same floor that the elevator is on; the condition for releasing the memory would be skipped. Each time this case occurs, more memory is leaked. Cases like this would not usually have any immediate effects.


1 Answers

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);
}
like image 90
Stefan Agapie Avatar answered Oct 05 '22 00:10

Stefan Agapie