NSURLResponse のアーカイブとアーカイブ解除がリークを引き起こしているコードでリークを見つけましたが、その理由がわかりません。
- (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
}
NSURLResponse を解凍すると、Instruments がリークを報告します。それをコメントアウトして使用しない場合、リークはありません。興味深いのは、代わりに NSURLResponse の断片を保存したことです。リークはありません:
// 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];
これがなぜなのか知っている人はいますか?NSURLResponse のアーカイブにバグがありますか、それとも何か間違っていますか?