Why does RKNSJSONSerialization crash on iOS 6?

2019-06-22 08:29发布

问题:

I'm seeing a crash in RKNSJSONSerialization on iOS 6 only - not on iOS 7. I'm using RestKit 0.20.3 and it happens fairly consistently for me. Even when I make the same request and get the same response for iOS 6/7, it works fine on iOS 7 but crashes on iOS 6.

Here's the crash - it's an EXC_BAD_ACCESS: http://crashes.to/s/2610b639062

The relevant (crashing) line in RestKit's RKNSJSONSerialization is the return:

+ (id)objectFromData:(NSData *)data error:(NSError **)error
{
    return [NSJSONSerialization JSONObjectWithData:data options:0 error:error];
}

So perhaps it's not RestKit at all - perhaps it's NSJSONSerialization.

I profiled the app with the Zombies tool and found this:

"An Objective-C message was sent to a deallocated 'CFString (immutable)' object (zombie) at address: 0x16851250."

Am I doing something wrong?

回答1:

I've resolved this. The issue was that my JSON had duplicate keys in it, and iOS 6 couldn't handle that. The solution is to remove the duplicate keys from the JSON before trying to parse it with NSJSONSerialization on iOS 6. Apparently Apple has resolved this issue on iOS 7, since it doesn't crash there.

Related: NSJSONSerialization bug?



回答2:

This seems to be a typo.

Assuming there is an object called error of type NSError, the call JSONObjectWithData: takes a pointer to a pointer as the last argument, i.e. with &.

return [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];