Question

I am new to iOS development. I am facing problems while running a background thread. In my code resetUi is running on main UI thread, Now I am starting a background thread to fetch an image data and update my image. Everything works fine, but memory is leaking when call is made to performSelectorInBackground.

Please let me know where I am doing wrong. Also please suggest if there is a better way to update my image while fetching from an URL(dataWithContentsOfURL).

[Update]

Instrument is displaying 2 separate leaks one at perfromSelectorInBackground and other at UIImage imageWithData. I guess something is going terribly wrong with imageupdate(imageWithData)

-(void)updateData{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];     

    profileName.text = oAuthTwitter.screen_name;

    if(profilePic.image == nil){
        NSString *urlString = @"https://api.twitter.com/1/users/profile_image/";
        urlString = [urlString stringByAppendingFormat:oAuthTwitter.screen_name];   
        urlString = [urlString stringByAppendingFormat:@"?size=bigger"];            
        profilePic.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:urlString]]];          
        [activityIndicator stopAnimating];
        [activityIndicator release];
    }   
    [pool drain];   

}   

- (void)resetUi{

    if (oAuthTwitter.oauth_token_authorized) {
        profilePic.hidden = NO;
        profileName.hidden = NO;

        NSLog(@"Resetting to authorised state");
        [self performSelectorInBackground:@selector(updateData) withObject:nil];

    }else{

        NSLog(@"Resetting Twitter UI to non-authorized state.");

    profilePic.hidden = YES;
        profileName.hidden = YES;       

    }

}   
Was it helpful?

Solution

I think you should use

[pool release];

rather than

[pool drain];

which is far more better practice.

Also can you try to release activityIndicator in the main thread?

From the code you have given I can't find any other cause for leak.. Have you tried to run your code using leak instrument and static analyzer?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top