iPhone JSON object releasing itself?

Posted by MidnightLightning on Stack Overflow See other posts from Stack Overflow or by MidnightLightning
Published on 2010-05-03T16:55:15Z Indexed on 2010/05/03 16:58 UTC
Read the original article Hit count: 538

Filed under:
|

I'm using the JSON Framework addon for iPhone's Objective-C to catch a JSON object that's an array of Dictionary-style objects via HTTP. Here's my connectionDidFinishLoading function:

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
   [connection release];
 NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
 [loadingIndicator stopAnimating];

 NSArray *responseArray = [responseString JSONValue]; // Grab the JSON array of dictionaries
 NSLog(@"Response Array: %@", responseArray);
 if ([responseArray respondsToSelector:@selector(count)]) {
  NSLog(@"Returned %@ items", [responseArray count]);
 }
 [responseArray release];
 [responseString release];
}

The issue is that the code is throwing a EXC_BAD_ACCESS error on the second NSLog line. The EXC_BAD_ACCESS error I think indicates that the variable got released from memory, but the first NSLog command works just fine (and shows that the data is all there); it seems that only when calling the count message is causing the error, but the respondsToSelector call at least thinks that the responseArray should be able to respond to that message. When running with the debugger, it crashes on that second line, but the stack shows that the responseArray object is still defined, and has 12 objects in it (so the debugger at least is able to get an accurate count of the contents of that variable).

Is this a problem with the JSON framework's creation of that NSArray, or is there something wrong with my code?

© Stack Overflow or respective owner

Related posts about iphone

Related posts about objective-c