Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSONkit with arrays and objectatindex #27

Closed
andrewtgraydc opened this issue May 27, 2011 · 15 comments
Closed

JSONkit with arrays and objectatindex #27

andrewtgraydc opened this issue May 27, 2011 · 15 comments

Comments

@andrewtgraydc
Copy link

Does JSONkit work with arrays? Ive got the following and using NSLogs I seem to have gotten my data correctly, but I am having some issues with tableview and the errors are new to me.

NSString *myJSON = @"http://www.example...";
NSData *jsonData = [NSData dataWithContentsOfURL:[NSURL URLWithString:myJSON]];
JSONDecoder *jsonKitDecoder = [JSONDecoder decoder];

myArray = [jsonKitDecoder objectWithData:jsonData]; //an array i declared in .h

the feed is an array of dictionaries and I've been able to use nested objectForKey, valueForKey and objectATIndex messages to get to all the data I want. I've even gotten it displayed in my tableview when the app first opens. The problem is as soon as i try and scroll the app crashes.

In the code editor, I get "Thread 1: Program received signal: "SIGABRT" on a line in titleForHEaderInSection: return [[myArray objectAtIndex:section] valueForKey:@"key"]; I've got the following right above that and that outputs to the log correctly, so its something to do with the scrolling and going to the next section thats off screen: NSLog(@"classser %@", [[[industries objectAtIndex:section] valueForKey:@"industry"]description]);

In the console I get "Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid parameter not satisfying: (objects != NULL) && (count <= capacity) && (objects[objectIndex] != NULL)'". It takes me to line 745 of JSONKit.m which is an assertion in obejctatIndex ( NSParameterAssert((objects != NULL) && (count <= capacity) && (objects[objectIndex] != NULL)); ) I'm not adhering to, but I just don't know how as everything else I do shows my array and dictionaries as working.

Let me know what else you need as I'm still new to all this

@johnezang
Copy link
Owner

The NSInternalInconsistencyException exception message is clearly generated by JSONKit- JSONKit has detected a condition which, if things are working correctly, "should not happen". Because the assertion check has multiple conditions that it checks, it's hard to tell exactly which one of the checks failed. If I had to guess, the most likely would be objects[objectIndex] != NULL, which would translate in to prose "The array in question should have a valid object at this index, but for some unexpected reason it is NULL".

If you could, could you please add to this bug report the JSON that was used / parsed that is causing this problem? Hopefully that will be enough for me to determine where things went wrong inside JSONKit and put together a fix for you. If it's not, I may need a bit more information from you.

Also, are the arrays and dictionary in question mutable? And if so, are you performing mutating operations on them, such as adding or removing items?

And since you are using a tableview, are you using a NSArrayController and/or bindings to display the contents of the array?

@ghost ghost assigned johnezang May 27, 2011
@johnezang
Copy link
Owner

Also, could add the following just before line 745, where the NSAssertion is:

  if(!((objects != NULL) && (count <= capacity) && (objects[objectIndex] != NULL))) {
    NSLog(@"objects: %p", objects);
    NSLog(@"count: %ld, capacity: %ld", count, capacity);
    NSLog(@"objectIndex: %ld, objects[objectIndex]: %p", objectIndex, (objects != NULL) ? objects[objectIndex] : NULL);
  }
  NSParameterAssert((objects != NULL) && (count <= capacity) && (objects[objectIndex] != NULL)); // Line 745

Then, run your app, and when the condition that causes the NSAssertion to fail, it should print out a bunch of info to the console that will help determine exactly what has gone wrong.

@andrewtgraydc
Copy link
Author

Thanks so much for the response, I really appreciate it! everything else seems great so far and i picked JSONkit for the performance. Ill get the info you asked for and reply in a bit. thanks again!!

@andrewtgraydc
Copy link
Author

ok i was able to hover over the variables in the three part assertion...count shows as 25 as does capacity so okay there. you're right, objects might be the issue. hovering over it shows "id * objects 0x0" - where does this come from? NSloging shows me I have data in the arrays and dictionaries.

not sure if i can provide JSON itself, but i can work on creating dummy one that exactly mimics it with fake data

no mutable arrays or dictionaries.

not using nsarraycontroller, should i be?

i'll add the if you sent and run and send results next. thanks!!

@andrewtgraydc
Copy link
Author

heres from the console, looks like objects is empty?

2011-05-27 15:52:25.272 navtest2[27134:207] objects: 0x0
2011-05-27 15:52:25.273 navtest2[27134:207] count: 25, capacity: 25
2011-05-27 15:52:25.273 navtest2[27134:207] objectIndex: 0, objects[objectIndex]: 0x0

@johnezang
Copy link
Owner

That's very odd. objects is the JKArray (an internal JSONKit class) iVar that holds the pointer to the memory allocation where the objects in that array are kept. There are very few things that modify that variable- when the array is created, sometimes when it is mutated and needs to grow that allocation, and when the object is released. This makes me wonder if you might have accidently released the object but still have a reference to it. Can you try making the following modification (near line 717):

- (void)dealloc
{
  if(JK_EXPECT_T(objects != NULL)) {
    NSUInteger atObject = 0UL;
    for(atObject = 0UL; atObject < count; atObject++) { if(JK_EXPECT_T(objects[atObject] != NULL)) { CFRelease(objects[atObject]); objects[atObject] = NULL; } }
    free(objects); objects = (void *)0xbadc0de; // Change from NULL to (void *)0xbadc0de.
  }

  [super dealloc];
}

Also change line ~745, the same NSAssertion that you originally reported, to the following:

  NSParameterAssert((objects != NULL) && (objects != (void *)0xbadc0de) && (count <= capacity) && (objects[objectIndex] != NULL));

This will allow the assertion to catch the fact that objects is set to 0xbadcode instead of allowing it to pass through and cause a sig fault or something.

My suspicion is that you will make these changes, and objects will be set to 0xbadcode, and the only way that could happen is because you missed a -retain somewhere.

@johnezang
Copy link
Owner

Assuming that my hunch that this is a retain / release issue, you might want to take a look at enabling NSZombieEnabled. You can find information about how to enable it here.

Some more memory debugging links:

http://www.cocoadev.com/index.pl?NSZombieEnabled
http://developer.apple.com/library/mac/#technotes/tn2004/tn2124.html
http://developer.apple.com/library/mac/technotes/tn2004/tn2124.html#SECFOUNDATION

@johnezang
Copy link
Owner

@skinsfan00atg, any update on this issue? Did it end up being a retain / release problem, or do we still need to dig in to a possible problem with JSONKit?

@andrewtgraydc
Copy link
Author

Sorry had family visiting will try tonight, thanks again!

On May 30, 2011, at 4:57 AM, johnezangreply@reply.github.com wrote:

@skinsfan00atg, any update on this issue? Did it end up being a retain / release problem, or do we still need to dig in to a possible problem with JSONKit?

Reply to this email directly or view it on GitHub:
#27 (comment)

@andrewtgraydc
Copy link
Author

so sorry for the delay, it was a self. issue and now everything works great!

@BenHall
Copy link

BenHall commented Nov 1, 2011

Sorry to raise this again, but what do you mean by a self. issue? I'm experiencing the same problem.

@andrewtgraydc
Copy link
Author

I'll double check my resolution when I get back to my work computer tomorrow

On Nov 1, 2011, at 7:28 PM, Ben Hallreply@reply.github.com wrote:

Sorry to raise this again, but what do you mean by a self. issue? I'm experiencing the same problem.

Reply to this email directly or view it on GitHub:
#27 (comment)

@DavidBoyes
Copy link

I had the same problem, it was a retain/release problem for me.

@adambemowski
Copy link

Had the same issue, I was trying to set the output straight to a dictionary I had created.
myDictionary = [string objectFromJSONString];

solved it by setting the output to a temporary dictionary and making a copy
NSDictionary *tempDictionary =[string objectFromJSONString];
myDictionary = [tempDictionary copy];

JSONKit is a very nice tool I will continue to use it!

@andrewtgraydc
Copy link
Author

BenHall, did you ever fix yours?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants