NSDictionary set in body of NSMutableURLRequest still remains in the memory even after the NSURLSessionDataTask is done.

Implemented post task using NSURLSession and was sending a dectionary in the body of NSMutableURLRequest. The Strings in the dictionary stays in the memory even after the task is done.


NSError *error;


NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];

NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];

NSURL *url = [NSURL URLWithString:@"http://127.0.0.1:3030/validate"];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url

cachePolicy:NSURLRequestReloadIgnoringCacheData

timeoutInterval:60.0];


[request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];

[request addValue:@"application/json" forHTTPHeaderField:@"Accept"];


[request setHTTPMethod:@"POST"];

NSMutableDictionary *mapData = [[NSMutableDictionary alloc] initWithObjectsAndKeys: @"darkhorse", @"usrname",

nil];

NSData *postData = [NSJSONSerialization dataWithJSONObject:mapData options:0 error:&error];

[request setHTTPBody:postData];



NSURLSessionDataTask *postDataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {


}];


[postDataTask resume];


Was able to dump the strings using below commands:

Step 1 : Connect lldb debugger to device/simulator

Step 2 : Import => "script import lldb.macosx.heap"

Step 3 : Search for strings "cstr_refs CSTRING "darkhorse"" after the NSURLSessionDataTask is done

Replies

Why do you put `__block` on a declaration of mapData?

Thanks OOPer for the reply.


Had tried without __block also. No luck. Was able to see the strings in the memory even after that.

I recommend that you explore this with Xcode’s memory graph feature; it’s great as showing you why something is still being referenced.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Thank you eskimo for the reply.


When i searched for the address of the string (found in heap memory) in Memory graph, it looked like NSMutableRequest was holding the data even though it is out of scope.


NSMutableURLRequest -> NSURLRequestInternal -> CFHTTPMessage -> NSMutableArray -> NSMutableDictionary(Storage) -> NSConcreteData




As the mapData used in the snippet was a local variable, its scope has ended with the execution of postDataTask .

The problem was that the strings in the dictionary that was set as a part of HTTPBody of NSMutableURLRequest still stays in the heap memory even after the task is done.


Was able to dump the strings using below commands:

Step 1 : Connect lldb debugger to device/simulator

Step 2 : Import => "script import lldb.macosx.heap"

Step 3 : Search for strings "cstr_refs CSTRING "darkhorse"" after the NSURLSessionDataTask is done

it looked like

NSMutableRequest
was holding the data even though it is out of scope.

So is the

NSMutableURLRequest
reported as a leak? If not, who’s holding on to it?

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"