views:

61

answers:

1

I'm implementing asynchronous image loading in UITableView, If I scroll rows fast my app crashes due to message sent to zombie... What is wrong am I doing here?

//loading image from URL
-(void)loadImageFromURL:(NSURL*)url {
    if (connection!=nil) { [connection release]; }
    //data is NSMutableData
    if (data!=nil) { [data release]; }

    NSURLRequest* request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
    connection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; 
}

//Append received data when it is received
- (void)connection:(NSURLConnection *)theConnection didReceiveData:(NSData *)incrementalData {
    if (data==nil) { data = [[NSMutableData alloc] init]; } 
        [data appendData:incrementalData]; //Message sent to zombie, app CRASHES HERE
}

//When finished
- (void)connectionDidFinishLoading:(NSURLConnection*)theConnection {
    //so self data now has the complete image 
    [connection release];
    connection=nil;
    //Use received data to construct image
    [data release]; 
    data=nil;
}
+5  A: 

Here:

if (connection!=nil) { [connection release]; }
if (data!=nil) { [data release]; }

you are releasing the data. Later you try using the released data so it crashes. Try this:

if (connection!=nil) { [connection release]; connection = nil; }
if (data!=nil) { [data release]; data = nil; }

That way your if statements will actually trigger.

Aleph
@Aleph Thanks a lot, you made my day!
Prashant