You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
There is a problem with the NSMutableDictionary returned by mutableObjectFromJSONString. A call to removeObjectsForKeys will remove the objects but not the keys. So we end up with nil objects. I also noticed side effects on other dictionary values that should not be affected.
Should it be overridden in the JKDictionary class like removeObjectForKey is?
Can you provide the JSON and some example code the demonstrates the problem?
From your description, it's not clear what the problem is. When you say "A call to removeObjectsForKeys will remove the objects but not the keys. So we end up with nil objects.", this is the expected behavior- when a NSDictionary does not contain a key, -objectForKey: will return nil (aka NULL).
Instead of removing the key/value pair as expected, the key remains in the mutable dictionary but the value is replaced by a null value. If I print the resulting object I can see: "myKey"=(null).
The behavior you describe is the expected behavior. The key does not "remain" in the dictionary, any key that does not exist in the dictionary returns NULL (aka nil). There are a number of internal checks in the form of NSCParameterAssert that ensure that every key in the dictionary contains a non-NULL object associated with it. If you did not compile JSONKit with NS_BLOCK_ASSERTIONS, these checks are enabled, and would have caused an exception, which would have been logged to the console.
I suggest you try creating a NSMutableDictionary with a number of keys/objects. Remove one of the keys, and see if the NSMutableDictionary has the same behavior for the removed key.
@omarkilani, the line you mentioned in your comment is one half of the problem. The real problem is in the removal logic- it (should have) "re-added" the items following the removed entry so that linear probe hash collisions didn't result in a NULL gap in the middle of the linear probe hash collision.
Your example code was very useful- it happened to create a dictionary with 3 items with a capacity of 3 items, and a, b, and c all just happened to be ≡ 0 mod 3. Sort of a "perfect storm" to exercise this bug.
I checked in a bug fix for this issue (used the new github issues feature to close this issue automagically with "Closes #17").
@johnezang, Thanks for the fix. Yeah, the real problem became obvious once I started delving in a bit more. I seem to get pretty lucky with just the right dictionaries to trigger things. :)
Activity
johnezang commentedon Apr 19, 2011
Can you provide the JSON and some example code the demonstrates the problem?
From your description, it's not clear what the problem is. When you say "A call to removeObjectsForKeys will remove the objects but not the keys. So we end up with nil objects.", this is the expected behavior- when a
NSDictionarydoes not contain a key,-objectForKey:will returnnil(akaNULL).jfmorin commentedon Apr 21, 2011
Instead of removing the key/value pair as expected, the key remains in the mutable dictionary but the value is replaced by a null value. If I print the resulting object I can see: "myKey"=(null).
johnezang commentedon Apr 21, 2011
The behavior you describe is the expected behavior. The key does not "remain" in the dictionary, any key that does not exist in the dictionary returns
NULL(akanil). There are a number of internal checks in the form ofNSCParameterAssertthat ensure that every key in the dictionary contains a non-NULLobject associated with it. If you did not compile JSONKit withNS_BLOCK_ASSERTIONS, these checks are enabled, and would have caused an exception, which would have been logged to the console.I suggest you try creating a
NSMutableDictionarywith a number of keys/objects. Remove one of the keys, and see if theNSMutableDictionaryhas the same behavior for the removed key.omarkilani commentedon Apr 22, 2011
Hey John,
I think what the OP is seeing is this (with JSONKit 2e6ccf4):
Code:
Output:
omarkilani commentedon Apr 22, 2011
Hey John,
I've tracked this down to the fix which fixed issue #8 :)
524b9d7
Specifically this line:
524b9d7#L0R1014
This comment:
// If the key was in the table, we would have found it by now.
Is incorrect if removeObjectForKey was used (like my example above).
dictionary->entry looks like this with the example:
[0] = {keyHash = 1062, key = @"a", object = "1"}
[1] = {keyHash = 0, key = NULL, object = NULL}
[2] = {keyHash = 1068, key = @"c", object = "3"}
So the loop terminates with NULL due to [1], but we need to keep going to find [2].
johnezang commentedon Apr 22, 2011
@omarkilani, the line you mentioned in your comment is one half of the problem. The real problem is in the removal logic- it (should have) "re-added" the items following the removed entry so that linear probe hash collisions didn't result in a
NULLgap in the middle of the linear probe hash collision.Your example code was very useful- it happened to create a dictionary with 3 items with a capacity of 3 items, and
a,b, andcall just happened to be ≡ 0 mod 3. Sort of a "perfect storm" to exercise this bug.I checked in a bug fix for this issue (used the new github issues feature to close this issue automagically with "Closes #17").
jfmorin commentedon Apr 22, 2011
Thanks for the help reproducing the problem and fixing it.
Good job!
omarkilani commentedon Apr 23, 2011
@johnezang, Thanks for the fix. Yeah, the real problem became obvious once I started delving in a bit more. I seem to get pretty lucky with just the right dictionaries to trigger things. :)
Thanks again.
Merge branch 'master' of github.com:playup/JSONKit
Fixes a bug when removing items from a JKDictionary. Since JKDictiona…