views:

71

answers:

1
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];
}

This code has a memory leak: it does not release memory of NSdata and after some time memory utilization of application reaches 61 MB. Can anyone help me get out of this?

A: 

Not 100% sure, but it probably has to do with the use of the "convenience constructor" with the NSData class in particular. When you call "dataWithContentsOfURL" you'll get back an NSData object that is automatically auto-released. However, your current NSAutoreleasePool might not be in a scope that will result in that memory being released until the application exits. You could try switching back to the alloc/init call you have commented out, and try manually releasing each NSData object inside the loop, to guarantee that the NSData memory is released for each instance of NSData created in the loop (after you've saved off the NSData to file).

Andy White