简体   繁体   中英

Memory leak Using NSData from URL

I receive Image from URL by threading . but memory leaking in NSData. Why? How do I fix it? Leaking in Iphone devie not in simulator. help me!!

//viewcontroller

-(void)viewDidLoad
{

[self performSelectorInBackground:@selector(loadImageScreenshot:) withObject:args];

}

// loding image

-(void) loadImageScreenshot:(NSDictionary *) args
{
 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 
 UIImage * screenshotImage=[UIImage imageWithStringURL:url];
 NSDictionary *args2=[NSDictionary dictionaryWithObjectsAndKeys:
                    [NSNumber numberWithInt:num], @"screenNum",
                    screenshotImage,@"image",
                    nil];                                                               

[self performSelectorOnMainThread:@selector(assignImageToScreenshotImageView:) withObject:args2  waitUntilDone:YES];

[pool release];

}

//image add

- (void) assignImageToScreenshotImageView:(NSDictionary *)arg

{ 

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
UIImage * image= [arg objectForKey:@"image"];
UIImageView *imageview=[UIImageView alloc]init];
               .
               .
 imageview.image=image;
[self.mScreenshotSpace addSubview:imageview];
[imageview release];
 [pool release];
 }

//image from url

+(UIImage *)imageWithStringURL:(NSString *)strURL
{
 NSURL *url =[NSURL URLWithString:strURL];
 NSData *   data=[[NSData alloc]initWithContentsOfURL:url options:NSDataReadingUncached error:&error];

 UIImage * image=[UIImage imageWithData:data ];
 [data release];
 return image;
 }

How do you know it is leaking? You are creating an autorelease pool in - (void) assignImageToScreenshotImageView:(NSDictionary *)arg that is never drained, that is a leak. Otherwise the code seems fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM