I'm having an issue where my app crashes due to a memory problem. I have an NSThread, a while loop within this one, and within the while loop, an if statement. I have two autorelease pools, but since I don't understand the concept of memory management too well, I don't know how to stop the leaking. My loop runs about 100 times maximum.
Here's my code:
Thank you very much in advance!
Here's my code:
Code:
-(void)startTheBackgroundJob {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];
[NSThread sleepForTimeInterval:6];
int i = 0;
BOOL done = NO;
while (!done) {
NSAutoreleasePool *pool2 = [[NSAutoreleasePool alloc] init];
if (clicked == YES) {
[pool release];
pool = [[NSAutoreleasePool alloc] init];
i++;
UIImage *screenshot = [UIImage imageWithCGImage:UIGetScreenImage()];
NSData *imageData = UIImagePNGRepresentation(screenshot);
NSString *fileName = [NSString stringWithFormat:@"image%d.png",i];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
documentsDirectory = [documentsDirectory stringByAppendingPathComponent:fileName];
[imageData writeToFile:documentsDirectory atomically:YES];
[pool2 release];
}
else {
done = YES;
}
}
[pool release];
}
Thank you very much in advance!