Note that there are some explanatory texts on larger screens.

plurals
  1. POiOS - NJSONSerialization causing a memory leak??(according to Instruments)
    text
    copied!<p>Below is the method with the code, where I am getting a memory leak using manual memory management. The memory leak is detected by using Xcode instruments and specifically points to the line where I am using NSJSONSerialization. I am running the target app (on a device with iOS 6.1). </p> <p>The first time that i tap on the refreshButton there is no leak. Any subsequent tap generates the leak(and more leaks on top of that if i continue tapping the button). Below is the code - This is basic stuff for consuming JSON web services(the web service link is bogus but the real one that I am using works). You will notice that I am using Grand Central Dispatch so that I can update the UI without waiting for the parsing of the JSON to finish. </p> <p>The line detected by instruments is surrounded by the asterisks. I would like to get some help to anyone who might have an idea of what is going on here. The full stack trace(as mentioned in the below comments i will put here:)</p> <blockquote> <p>+(NSJSONSerialization JSONObjectWithData:option:error:] -> -[_NSJSONReader parseData:options:] -> -[_NSJSONReader parseUTF8JSONData:skipBytes:options]->newJSONValue->newJSONString->[NSPlaceholde‌​rString initWithBytes:length:encoding:]</p> </blockquote> <pre><code>-(void)parseDictionary:(NSDictionary *)dictonary{ self.transactions = [dictonary objectForKey:@"transactions"]; if(!self.transactions){ NSLog(@"Expected 'transactions' array"); return; } for (int arrayIndex = 0; arrayIndex &lt; [self.transactions count]; arrayIndex++) { TransactionResult *result = [[[TransactionResult alloc] init] autorelease]; result.transactionID = [[self.transactions objectAtIndex:arrayIndex] objectForKey:@"ID"]; result.transactionDescription = [[self.transactions objectAtIndex:arrayIndex] objectForKey:@"description"]; result.transactionPrice = [[self.transactions objectAtIndex:arrayIndex] objectForKey:@"price"]; self.totalPrice += [result.transactionPrice doubleValue]; NSLog(@"total price: %f", self.totalPrice); [self.transactionResults addObject:result]; result = nil; } </code></pre> <p>}</p> <pre><code> - (IBAction)refreshButtonPressed:(UIBarButtonItem *)sender { __block id resultObject; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),^{ NSURL *url = [NSURL URLWithString:@"http://mywebservice.php"]; NSData *data = [NSData dataWithContentsOfURL:url]; NSError *error; ***resultObject = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&amp;error];*** if(!error){ if([resultObject isKindOfClass:[NSDictionary class]]){ NSDictionary *dictonary = resultObject; [self parseDictionary:dictonary]; NSLog(@"Done parsing!"); dispatch_async(dispatch_get_main_queue(), ^{ self.isLoading = NO; [self.transactionsTableView reloadData]; }); } else{ NSLog(@"JSON Error: Expected Dictionary"); resultObject = nil; return; } } else{ NSLog(@"JSON Error: %@", [error localizedDescription]); dispatch_async(dispatch_get_main_queue(), ^{ resultObject = nil; [self.transactionsTableView reloadData]; [self showError]; }); return; } }); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload