NSURLConnection scheduleInRunLoop'leak'

时间:2013-03-28 12:24:11

标签: ios memory-leaks automatic-ref-counting nsurlconnection nsurlrequest

我正在使用NSURLConnection异步发布表单数据。使用工具我可以看到启动连接时内存使用量增加,但一旦连接完成,内存使用量就会下降到高于前一个基线的水平。我将此称为“泄漏”,因为在连接开始/结束后大约60秒,内存被释放,图形回落到原始基线。

为什么这个记忆暂时保留?我怎样才能立即释放它?

- (id)initWithPostURL:(NSURL *)url content:(NSData *)content completionBlock:(void (^)(NSData *data))completionBlock boundary:(NSString *)boundary;
{
    self = [super init];
    if (self) {        
        NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:30.0f];
        [urlRequest setHTTPMethod:@"POST"];
        [urlRequest setValue:[NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary] forHTTPHeaderField:@"Content-type"];
        [urlRequest setValue:[NSString stringWithFormat:@"%d", content.length] forHTTPHeaderField:@"Content-length"];
        [urlRequest setHTTPBody:content];

        _urlConnection = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self startImmediately:NO];
        [_urlConnection scheduleInRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
        [_urlConnection start];
        _data = [NSMutableData data];
        _dataSemaphore = NULL;
        _completionBlock = completionBlock;
    }
    return self;
}

0 个答案:

没有答案