0

enter image description hereenter image description hereI am working on JSON data parsing with lots of images downloading and data parsing.I have following code for parsing

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
  {   

     NSString *responseString = [[NSString alloc] initWithData:webdata encoding:NSASCIIStringEncoding];
     [webdata release];
     [connection release];
     NSDictionary *values = [(NSDictionary*)[responseString JSONValue] objectForKey:@"UserId"];
     NSDictionary *Active = [(NSDictionary*)[responseString JSONValue] objectForKey:@"Active"];
     [responseString release];
     NSString *UserID=[NSString stringWithFormat:@"%@",values];
     NSString *Status=[NSString stringWithFormat:@"%@",Active];
     [WSDelegate WServiceResponseMsg:@"WS_Authenticate_User" withResponse:UserID forParam:Status];
}

I have many classes with above code for parsing but app crashes after some time interval because of SBJSON parser.In instrument it gives app crashed because of low memory warning.enter image description here

M007
  • 580
  • 1
  • 5
  • 24
  • Consider using ARC (Automatic Reference Counting). There is no reason not to. It makes coding for iOS much more convenient, and it would most likely solve the problem you're having. – Andreas Ley Oct 29 '12 at 13:43
  • SBJson 3.1 *requires ARC* or it will leak. This is *not* a bug in SBJson. It is well documented here: https://github.com/stig/json-framework/blob/master/NEWS.md – Stig Brautaset Dec 12 '12 at 15:08

3 Answers3

1

It is a very wrong assumption that most of the developers have while using SBJSONParser that, it has memory leaks. SBJSONParser does not has any leaks and does not introduces leaks in your code. It is true that INSTRUMENTS tells you that the leak is because of SBJSONParser, but it denotes something else. Leaks are because of the way you have implemented SBJSONParser APIs. You must have done something wrong in your code.

Go to the leaks in your instruments. Open Extended Details toolbar and see the line of code that has leak. Instruments tells you the nearest place where the leak is.

Evol Gate
  • 2,247
  • 3
  • 19
  • 37
  • yes you are right.Issue is in implementing SBJSON parser.It gives low memory warning in instrument's extended detail bar because of following line NSDictionary *values = [(NSDictionary*)[responseString JSONValue] objectForKey:@"UserId"]; But am not know where my code is wroung in memory management? – M007 Oct 29 '12 at 13:21
  • whata re you doing with the dictionaries values and Active. I am pretty sure issue is with these two dictionaries – Evol Gate Oct 29 '12 at 13:33
0

Better option would be to use NSJSONSerialization that come as a part with iOS 5 and above

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
  {  
    NSMutableDictionary *values = [NSJSONSerialization JSONObjectWithData:receivedData options:NSJSONReadingMutableContainers error:&error];
}
AppleDelegate
  • 4,269
  • 1
  • 20
  • 27
  • I have implemented code as you suggest me and I am facing leaks in that.I have uploaded image of instrument after implementing code that you gave me above but it gives 100% memory leak in below line so can you please tell me where I am wrong with memory management? – M007 Oct 30 '12 at 04:36
0

Finally got solution.Just use below line for JSON parsing.Remove NSMutableDictionary and use id :

 NSError *jsonError = nil;

 id allValues = [NSJSONSerialization JSONObjectWithData:webdata
                                                   options:0
                                                     error:&jsonError];

 NSArray *array = [allValues objectForKey:@"Contestants"];
M007
  • 580
  • 1
  • 5
  • 24