僵尸杀了我的应用程序

时间:2010-10-22 18:01:48

标签: iphone objective-c memory ios4

我正在UITableView中实现异步图像加载,如果我快速滚动行,我的应用程序因为发送给僵尸的消息而崩溃...我在这里做错了什么?

//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;
}

1 个答案:

答案 0 :(得分:5)

下面:

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

您正在发布数据。稍后您尝试使用已发布的数据,以便崩溃。试试这个:

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

这样,您的if语句实际上会触发。