简体   繁体   中英

iOS NSURLConnection memory leak

i got code like this

#import "UIWebImageView.h"

@interface UIWebImageView (hiddenMethods)

- (void) initDefaults;

@end

@implementation UIWebImageView (hiddenMethods)

- (void) initDefaults
{
    self.showActivityIndicator = NO;
    self.activityIndicatorStyle = UIActivityIndicatorViewStyleWhite;
    self.activityIndicatorSize = CGSizeMake(20.0, 20.0);
    //data = [[NSMutableData alloc] init];
}

@end

@implementation UIWebImageView

@synthesize showActivityIndicator;
@synthesize activityIndicatorStyle;
@synthesize activityIndicatorSize;

- (id) init
{
    if (self = [super init])
    {
        [self initDefaults];
    }
    return self;
}

- (void) loadFromURL:(NSString *) url
{
    [self.image release];
    self.image = nil;
    request = [NSURLRequest requestWithURL:[NSURL URLWithString:url] 
                                             cachePolicy:NSURLRequestReturnCacheDataElseLoad
                                         timeoutInterval:30.0];

    if (connection == nil)
        connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

    // activity indicator start
    indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:activityIndicatorStyle];
    indicator.frame = CGRectMake((self.frame.size.width - activityIndicatorSize.width)/2, (self.frame.size.height - activityIndicatorSize.height)/2,
                                 activityIndicatorSize.width, activityIndicatorSize.height);

    [self addSubview:indicator];
    [indicator startAnimating];
}

- (void) dealloc
{
    [data release];
    [indicator release];
    [connection release];
    [super dealloc];
}

#pragma mark -
#pragma mark connection delegate

- (void) connection:(NSURLConnection *)theConnection didReceiveData:(NSData *)incrementalData 
{

    if (data == nil)
        data = [[NSMutableData alloc] initWithCapacity:2048];

    [data appendData:incrementalData];
}

- (void) connectionDidFinishLoading:(NSURLConnection *)theConnection 
{
    self.image = [UIImage imageWithData:data];

    [indicator removeFromSuperview];
    //data = nil;
    [data release], data = nil;
    [connection release], connection = nil;
    [indicator release], indicator = nil;
}

- (void) connection:(NSURLConnection *)theConnection didFailWithError:(NSError *)error
{
    self.image = [UIImage imageNamed:@"icon.jpg"];
    [indicator removeFromSuperview];
    //data = nil;
    [data release], data = nil;
    [connection release], connection = nil;
    [indicator release], indicator = nil;
}

I'm using this for downloading images from web. one image for every cell in in table. and it works fine when image is not going out of the screen. BUT when u scrolling tableView fast and some images did not finished loading while they are on screen there huge memory leaks.

i know where is the leak and why its leaking. but i can't find solution. any thoughts ? thank you

PS sorry for my english

UPDATE

here is a code for adding images into tableView

UIWebImageView *tmpImageView = [[UIWebImageView alloc] initWithFrame:CGRectMake(0, 0, 57, 76)];
    tmpImageView.showActivityIndicator = YES;
    tmpImageView.contentMode = UIViewContentModeScaleAspectFit;
    tmpImageView.activityIndicatorStyle = UIActivityIndicatorViewStyleGray;
    [tmpImageView loadFromURL:[[tableArray objectAtIndex:indexPath.row] objectForKey:@"picurl"]];
    [cell addSubview:tmpImageView];
    [tmpImageView release];

and I'm repeating = ) its leaking only when it not finished loading and went off the screen while scrolling

The leaks are happening due to the allocation of UIWebImageView objects recursively (considering you are using reusable cells).

you should change your code to:

    UIWebImageView *tmpImageView = [cell viewWithTag:2011];

    if(!tmpImageView)
    {
    tmpImageView = [[UIWebImageView alloc] initWithFrame:CGRectMake(0, 0, 57, 76)];
        tmpImageView.showActivityIndicator = YES;
        tmpImageView.tag = 2011;
        tmpImageView.contentMode = UIViewContentModeScaleAspectFit;
        tmpImageView.activityIndicatorStyle = UIActivityIndicatorViewStyleGray;
        [cell addSubview:tmpImageView];
        [tmpImageView release];
    }


        [tmpImageView loadFromURL:[[tableArray objectAtIndex:indexPath.row] objectForKey:@"picurl"]];

you have to handle for the showActivityIndicator thing some how...based upon your code but the above mentioned change will remove your memory leaks.

Autorelase your NSURLConnection with this syntax

connection = [NSURLConnection connectionWithRequest:request delegate:self];

and delete your [connection release], connection = nil;

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