0
2G iPod touchで大きすぎて1枚でできないファイルを解析しています。私はNSData/NSMutableStringの部分を除いてすべてのコードをコメントアウトし、stilには漏れがあります。私は理解していない....ファイルからチャンクを読み込むためメモリリーク - NSDataとNSMutableString
支援方法:
+ (NSData *) dataWithContentsOfFile:(NSString *)path atOffset:(off_t)offset withSize:(size_t)bytes {
FILE *file = fopen([path UTF8String], "rb");
if(file == NULL)
return nil;
void *data = malloc(bytes); // check for NULL!
fseeko(file, offset, SEEK_SET);
fread(data, 1, bytes, file); // check return value, in case read was short!
fclose(file);
// NSData takes ownership and will call free(data) when it's released
return [NSData dataWithBytesNoCopy:data length:bytes];
}
をし、漏れたベアボーンコード:
while(cnt<total) {
NSAutoreleasePool* pool= [[NSAutoreleasePool alloc] init];
NSData* data= [NSData dataWithContentsOfFile:fullPath atOffset:cnt withSize:MIN(100000,total-cnt)];
NSString* xmlFragment = [[NSString alloc] initWithBytes: [data bytes] length:[data length] encoding: NSUTF8StringEncoding];
cnt+= [xmlFragment length];
[pool drain];
}
参考資料として、[Objective-Cの所有権の仕組み](http://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmObjectOwnership.html)を読む価値があるかもしれません。 。 – nil
ありがとう!私はNSAutoreleasePoolがすべてを夢中にしたと思った。これは分かりやすくするために、メソッドの小さなサブセットです。私は何時間もXML解析のバグを追跡しました。 – stevesch