0

每次我都会通过调用下面的方法上传一段2Mb左右的数据。

调用大约 30 次后,应用程序将收到内存警告,然后大约 20 次后,应用程序将崩溃而没有任何信息。我​​的代码有什么问题吗?我没有在我的代码中发现任何内存泄漏。

//connect to uploading server, upload one slice of data, and get the next slice uploading point.
-(void)uploadSlice
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    NSURL *baseUrl = [NSURL URLWithString:[NSString stringWithFormat:@"http://%@/gupload/upload_slice",_uploadServerIP]];
    NSData *sliceData = nil;  
    NSFileHandle *file = [NSFileHandle fileHandleForReadingAtPath:item.filePath];  
    if (file == nil)  
        NSLog(@"Failed to open file");  

    [file seekToFileOffset:_offset];  
    sliceData = [file readDataOfLength:_length];  
    [file closeFile];
    NSMutableData *data = [NSMutableData dataWithData:sliceData];
    NSDictionary *dic    = [[NSDictionary alloc]initWithObjectsAndKeys:
                            _uploadToken,                                  @"upload_token",
                            [NSString stringWithFormat:@"%d",_sliceTaskId],@"slice_task_id",
                            [NSString stringWithFormat:@"%lld",_offset],   @"offset",
                            [NSString stringWithFormat:@"%d",_length],     @"length",
                            nil];

    NSURL *finalUrl = [self generateURL:[baseUrl absoluteString] params:dic];
    NSLog(@"upload slice url = %@",finalUrl);
    ASIFormDataRequest *aRequest = [ASIFormDataRequest requestWithURL:finalUrl];
    [aRequest setPostBody:data];

    aRequest.requestMethod = @"POST";
    [self setPostUserInfo:aRequest withRequestType:kYoukuUploadSlice];
    [_networkQueue addOperation:aRequest];

    [dic release];
    [pool drain];
}
4

1 回答 1

2

您一次排队太多请求 - ASIHTTPRequest 会将您的数据保留在内存中,直到将其发送到服务器,因此内存不足也就不足为奇了。

尝试确保队列中未处理的请求不超过 3 或 4 个 - 即。队列 4 开始,然后在一个完成后排队另一个,依此类推。

于 2012-06-18T02:13:18.863 回答