NSData over-retained after decoding from NSKeyedUnarchiver

Originator:philip.willoughby
Number:rdar://10425439 Date Originated:10-Nov-2011 11:27 AM
Status:Open Resolved:
Product:iPad Product Version:
Classification:Crash/Hang/Data Loss Reproducible:Always
 
Summary:

NSData objects decoded from an NSKeyedUnarchiver are returned with a net retain count of 1. They should be returned with a net retain count of +0 (i.e. autoreleased exactly as many times as they are retained). On the simulator the behaviour is correct, on the device it fails.

Steps to Reproduce:

-(void)someFunction
{
    @autoreleasepool
    {
        NSData *fileData = [NSData dataWithContentsOfFile:storagePath options:NSDataReadingUncached error:&error];
        MyThing *obj = [[NSKeyedUnarchiver unarchiveObjectWithData:fileData] retain];
    }
    [MyThing release];
}
…
@implementation MyThing
-(id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super init];
    if (self)
    {
        myData = [[aDecoder decodeObjectForKey:kDataKey] retain];
    }
    return self;
}

-(void)dealloc
{
    [myData release];
    [super dealloc];
}
@end

Call -someFunction and watch the heap growth/leaks instrument.

Expected Results:
Net heap growth is zero. Nothing is leaked.

Actual Results:
Net heap growth is the size of the decoded data object. The NSData object decoded from the archive is leaked.

Regression:
Unknown. Needs fixing anyway.

Notes:
There is no suitable workaround - if I insert an extra call to explicitly release it my app will explode if you fix this, or if it is run on a version of iOS before the problem was introduced.

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!