Reputation: 587
I'm using ios 7 and I need to do:
In separete thread I want to create image from network and put it into UIImageView. I need to do this every 200 ms.
My code looks like:
- (void)startPreview:(CGFloat)specialFramesRates;
{
if(isPreview)
return;
[Utils runOnThread:^{
[Log log:@"Start preview"]; //here we have a leak
isPreview = YES;
while(isPreview) {
[self getNewBitmap];
sleep(fpsTime);
if(!isPreview)
break;
if(checkAvabilityCam > 10)
break;
}
[Log log:@"Stoped preview"];
}];
}
- (void)getNewBitmap
{
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
[request setTimeoutInterval:1];
[request setHTTPMethod:@"GET"];
NSError *requestError;
NSURLResponse *urlResponse = nil;
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&requestError];
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
if(delegate && response) {
checkAvabilityCam = 0;
//TODO what I should do here?
UIImage *image = [UIImage imageWithData:newImage]; //HERE IS LEAK !!!!
[delegate onShowImage:response]; //here I show image in main thread
image = nil; //With or without doesn't work
return;
}
checkAvabilityCam++;
if(delegate)
[delegate onShowDefaultImage];
}
In this line of code I have a problem:
//TODO what I should do here?
UIImage *image = [UIImage imageWithData:newImage]; //HERE IS LEAK !!!!
[delegate onShowImage:response]; //here I show image in main thread
image = nil; //With or without doesn't work
What can I use instead of "[UIImage imageWithData:]" ? I tried save into file and load but with the same effect. What should I do?
Upvotes: 0
Views: 3185
Reputation: 4174
UIImage *image = [UIImage imageWithData:newImage]; //HERE IS LEAK !!!!
You are creating an autoreleased object here. Since you're doing this on a background thread, any autoreleased objects you create won't get released unless your thread has its own autorelease pool.
If you are using ARC, create an autorelease pool with the @autoreleasepool keyword:
@autoreleasepool {
UIImage *image = [UIImage imageWithData:newImage];
// Do stuff with image
}
If you're not using ARC, create an autorelease pool manually:
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
UIImage *image = [UIImage imageWithData:newImage];
// Do stuff with image
[pool release];
Upvotes: 4