NSMutableRequest对象的主体的已分配数据未被释放

时间:2013-04-22 15:10:14

标签: ios objective-c xcode memory-management xcode-instruments

在我的应用中,我必须执行多部分POST请求。在请求中,我必须发送存储在设备照片库中的文件。在完成上传并收到返回的数据后,我向请求发送了一条释放消息,但是仪器显示数据仍然存在于设备的内存中。

这就是我创建请求对象的方式:

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];

[request setHTTPShouldHandleCookies:YES];
[request setTimeoutInterval:30];
[request setHTTPMethod:@"POST"];
NSString *boundary = @"---------------------------14737809831466499882746641449";

// set Content-Type in HTTP header
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request setValue:contentType forHTTPHeaderField: @"Content-Type"];

// post body
NSMutableData *body = [[NSMutableData alloc] init];

// add parts (all parts are strings) `parts` is a NSDictionary object
for(id key in parts) {
    [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", key] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"%@\r\n", [parts objectForKey:key]] dataUsingEncoding:NSUTF8StringEncoding]];
}

// add files data - `file` is a ALAssetRepresentation object
NSData *fileData;
NSString *filename = [file filename];
Byte *buffer = (Byte*)malloc(file.size);
NSUInteger buffered = [file getBytes:buffer fromOffset:0.0 length:file.size error:nil];
fileData = [[NSData alloc] initWithBytesNoCopy:buffer length:buffered freeWhenDone:YES];




if (fileData) {
    [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

    if ([requestType isEqualToString:PUBLISH_TYPE_VC_MANDA]) {
        [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"attachment\"; filename=\"%@\"\r\n", filename] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    } else {
        [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"file\"; filename=\"%@\"\r\n", filename] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    }
    [body appendData:fileData];
    [body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
}

[fileData release];


[body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];


// setting the body of the post to the reqeust
[request setHTTPBody:body];

[body release];

// set the content-length
NSString *postLength = [NSString stringWithFormat:@"%d", [body length]];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"Keep-Alive" forHTTPHeaderField:@"Connection"];

// set URL `adress` is a NSURL object
[request setURL:address];

//set cookie
if (storedCookie) {

    NSDictionary *properties = [NSDictionary dictionaryWithObjectsAndKeys:[storedCookie name], NSHTTPCookieName, myDomain, NSHTTPCookieDomain,@"/",NSHTTPCookiePath, [storedCookie value], NSHTTPCookieValue, nil];

    NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:properties];
    NSArray* cookies = [NSArray arrayWithObjects: cookie, nil];
    NSDictionary * headers = [NSHTTPCookie requestHeaderFieldsWithCookies:cookies];
    [request setAllHTTPHeaderFields:headers];
}

在此之后,在收到响应后,我执行[request release],但分配的字节保留在内存中。

我在分配模式下运行了Instruments,它声称有一个类别为Malloc 91MB的对象(我正在上传的ALAsset的大小)。通过查看调用树,占用所有内存的符号在上述方法中为[NSConcreteMutableData appendBytes:length:]

为什么不释放字节?更重要的是,为什么必须将数据移动到设备的内存中?我见过像Facebook这样的应用程序从画廊上传非常大的视频而没有任何内存问题。

非常感谢您的关注,非常感谢任何帮助或指示

编辑:进一步的调查显示,当我使用[request release]时,请求对象被释放,但它的身体却没有。非常奇怪的行为......

EDIT2:我明确使用了[request.HTTPBody release];并释放了字节。我仍然无法相信[request release]没有释放它的身体。这可能是个错误吗?

2 个答案:

答案 0 :(得分:1)

您的请求的retainCount为2:

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; // 1

...

[request retain]; // 2

然后,如果你[request release],它会减少一个。

解决方案不是[request retain]

答案 1 :(得分:0)

经过进一步调查后,内存未被释放,因为需要手动释放请求的属性HTTPBody(参见我的第二次编辑)。

对于在NSRequest中发送大块数据,有一种不会消耗设备内存的简单方法。您必须将文件写入磁盘,然后映射它。我在this SO answer详细描述了它。希望它可以帮助某人

相关问题