0
for (int i=0; i<[images count] ;i++) {
    url=@"http://192.168.0.101/titan/titanimages/";
    url=[url stringByAppendingString:[images objectAtIndex:i]];
    //NSData *imageData=[[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:url]];
    NSData *imageData=[NSData dataWithContentsOfURL:[NSURL URLWithString:url]];
    destinationPath=[documentsDirectory stringByAppendingString:@"/modelimages"];
    destinationPath=[destinationPath stringByAppendingPathComponent:[images objectAtIndex:i]];

    [imageData writeToFile:destinationPath atomically:YES];

    value=value+divideValue;
    printf("%f\n",value);
    [NSThread detachNewThreadSelector:@selector(updateProgressBar)toTarget:self withObject:nil];
}

このコードにはメモリ リークがあります。NSdata のメモリを解放せず、しばらくするとアプリケーションのメモリ使用率が 61 MB に達します。誰かが私がこれから抜け出すのを手伝ってくれますか?

4

1 に答える 1

0

100%確実ではありませんが、特に NSData クラスでの「便利なコンストラクター」の使用に関係している可能性があります。「dataWithContentsOfURL」を呼び出すと、自動的に自動解放される NSData オブジェクトが返されます。ただし、現在の NSAutoreleasePool は、アプリケーションが終了するまでそのメモリが解放される結果となるスコープ内にない可能性があります。コメントアウトした alloc/init 呼び出しに切り替えて、ループ内の各 NSData オブジェクトを手動で解放してみてください。これにより、ループ内で作成された NSData のインスタンスごとに NSData メモリが解放されることが保証されます (保存した後)。 NSData からファイルへ)。

于 2009-08-25T07:48:16.260 に答える