views:

154

answers:

1

this is my code it does not releases memory it reaches to 60 mb and application kills

for (int i=0; i<[modelList count] ;i++) {

 url=@"http://192.168.0.101/images/projectimages/";
 url=[url stringByAppendingString:[modelList objectAtIndex:i]];
 url=[url stringByAppendingString:@".jpg"];


 [[NSURLCache sharedURLCache] setMemoryCapacity:0];
 [[NSURLCache sharedURLCache] setDiskCapacity:0];




 NSData *imageData=[[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:url]];

 destinationPath=[documentsDirectory stringByAppendingString:@"/modelimages"];
 destinationPath=[destinationPath stringByAppendingPathComponent:[modelList objectAtIndex:i]];
 destinationPath=[destinationPath stringByAppendingString:@".jpg"];

 [imageData writeToFile:destinationPath atomically:YES];

 [imageData release];
 //imageData=nil;
 //[myurl release]; 

 //[imageData release]; 

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

}
+2  A: 

This is terrible:

[NSThread detachNewThreadSelector:@selector(updateProgressBar)toTarget:self withObject:nil];

Because it:

  1. creates a lot of threads.
  2. as I expect it updates UI, while it should be updated from main thread only.

I think much better will be to do something like that:

[self performSelectorOnMainThread:@selector(updateProgressBar) 
                       withObject:nil // or any object you need to pass
                    waitUntilDone:NO] //

And the method you've given as example - it should be run in a separate thread instead. In this case you'll have one background thread doing all hard work and it will notify the main thread about user interface updates.

Valerii Hiora