I found a leak in my code where archiving and unarchiving an NSURLResponse was causing a leak, and I can't figure out why.
- (void)doStuffWithResponse:(NSURLResponse *)response {
NSMutableData *saveData = [[NSMutableData alloc] init];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:saveData];
[archiver encodeObject:response forKey:@"response"];
// Encode other objects
[archiver finishDecoding];
[archiver release];
// Write data to disk
// release, clean up objects
}
- (void)retrieveResponseFromPath:(NSString *)path {
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:[NSData dataWithContentsOfFile:path]];
NSURLResponse *response = [unarchiver decodeObjectForKey:@"response"];
// The above line leaks!!
// decode other objects
// clean up memory and do other operations
}
Instruments reports a leak when I unarchive the NSURLResponse. If I comment that out and do not use it, there is no leak. What was interesting was instead I saved the pieces of the NSURLResponse there is no leak:
// Encode:
[archiver encodeObject:[response URL] forKey:@"URL"];
[archiver encodeObject:[response MIMEType] forKey:@"MIMEType"];
[archiver encodeObject:[NSNumber numberWithLongLong:[response expectedContentLength]] forKey:@"expectedContentLength"];
[archiver encodeObject:[response textEncodingName] forKey:@"textEncodingName"];
// Decode:
NSURL *url = [unarchiver decodeObjectForKey:@"URL"];
NSString *mimeType = [unarchiver decodeObjectForIKey:@"MIMEType"];
NSNumber *expectedContentLength = [unarchiver decodeObjectForKey:@"expectedContentLength"];
NSString *textEncodingName = [unarchiver decodeObjectForKey:@"textEncodingName"];
NSURLResponse* response = [[NSHTTPURLResponse alloc] initWithURL:url MIMEType:mimeType expectedContentLength:[expectedContentLength longLongValue] textEncodingName:textEncodingName];
Anyone know why this is? Is there a bug with archiving NSURLResponse or am I doing something wrong?