NSJSONSerialization与NSCFNumber objCType崩溃

时间:2017-12-01 03:40:44

标签: ios objective-c nsmutablearray nsjsonserialization

Exception Type:  SIGTRAP
Exception Codes: TRAP_BRKPT at 0x1897e6880
Crashed Thread:  38

Thread 38 Crashed:
0   CoreFoundation                  0x00000001897e6880 -[__NSCFNumber objCType] :64 (in CoreFoundation)
1   Foundation                      0x000000018a47e86c +[_NSJSONReader validForJSON:depth:allowFragments:] :640 (in Foundation)
2   Foundation                      0x000000018a47ea44 __51+[_NSJSONReader validForJSON:depth:allowFragments:]_block_invoke.56 :68 (in Foundation)
3   CoreFoundation                  0x00000001897f1e34 __53-[__NSArrayM enumerateObjectsWithOptions:usingBlock:]_block_invoke :132 (in CoreFoundation)
4   CoreFoundation                  0x00000001897f1cc8 -[__NSArrayM enumerateObjectsWithOptions:usingBlock:] :208 (in CoreFoundation)
5   Foundation                      0x000000018a47e738 +[_NSJSONReader validForJSON:depth:allowFragments:] :332 (in Foundation)
6   Foundation                      0x000000018a47e99c __51+[_NSJSONReader validForJSON:depth:allowFragments:]_block_invoke :116 (in Foundation)
7   CoreFoundation                  0x000000018984ab68 __65-[__NSDictionaryI enumerateKeysAndObjectsWithOptions:usingBlock:]_block_invoke :104 (in CoreFoundation)
8   CoreFoundation                  0x00000001898392b8 -[__NSDictionaryI enumerateKeysAndObjectsWithOptions:usingBlock:] :216 (in CoreFoundation)
9   Foundation                      0x000000018a47e6b4 +[_NSJSONReader validForJSON:depth:allowFragments:] :200 (in Foundation)
10  InPlayFlight                    0x0000000100be1660 +[MyJsonWrapper convertToJsonDataWithDictionary:] :52 (in InPlayFlight)
11  InPlayFlight                    0x0000000100be16d4 +[MyJsonWrapper convertToJsonStringWithDictionary:] :32 (in InPlayFlight)
12  InPlayFlight                    0x0000000100bf12ec __41-[MySensorManager processDynamicInfo:]_block_invoke :1060 (in InPlayFlight)
13  InPlayFlight                    0x0000000100bcf33c -[MyDeviceSensorInfo checkAllRecords] :952 (in InPlayFlight)
14  libdispatch.dylib               0x00000001887ba9a0 _dispatch_client_callout :16 (in libdispatch.dylib)
15  libdispatch.dylib               0x00000001887c7604 _dispatch_continuation_pop :448 (in libdispatch.dylib)
16  libdispatch.dylib               0x00000001887d3c1c _dispatch_source_latch_and_call :204 (in libdispatch.dylib)
17  libdispatch.dylib               0x00000001887bc8a0 _dispatch_source_invoke :804 (in libdispatch.dylib)
18  libdispatch.dylib               0x00000001887c8fa8 _dispatch_queue_override_invoke :344 (in libdispatch.dylib)
19  libdispatch.dylib               0x00000001887caa50 _dispatch_root_queue_drain :540 (in libdispatch.dylib)
20  libdispatch.dylib               0x00000001887ca7d0 _dispatch_worker_thread3 :124 (in libdispatch.dylib)
21  libsystem_pthread.dylib         0x00000001889c3100 _pthread_wqthread :1096 (in libsystem_pthread.dylib)
22  libsystem_pthread.dylib         0x00000001889c2cac _start_wqthread :4 (in libsystem_pthread.dylib)

我将CMAccelerometerDataCMGyroData收集为NSNumber值并将它们放入数组中,然后将其转换为json字符串。似乎当我调用stopGyroUpdates时,它不会马上停止,因此底层的可变数组会随着json转换而改变。 gyroArrayaccArray NSMutableArrayNSNumber值组成。

NSTimeInterval ti = [[NSDate date] timeIntervalSince1970];

NSString* time = [NSString stringWithFormat:@"%.0f",ti];

NSDictionary* map = @{@"input_3":gyroArray,@"input_3_count":@(1000),@"input_4":accArray,@"input_4_count":@(1000),@"input_type_num":@(2),@"time":time};

NSString* jsonStr = [MyJsonWrapper convertToJsonStringWithDictionary:map];


+ (NSData *) convertToJsonDataWithDictionary:(NSDictionary *)data
{
    NSError *err = nil;
    NSData *jsonData = nil;

    if ([NSJSONSerialization isValidJSONObject:data]) {
        jsonData = [NSJSONSerialization dataWithJSONObject:data options:0 error:&err];
    } else {
        NSLog(@"not valid json!");
    }


    return jsonData;
}

+ (NSString *) convertToJsonStringWithDictionary:(NSDictionary *)data
{
    NSData *jsonData = [ASSJsonWrapper convertToJsonDataWithDictionary:data];
    NSString *strJson = nil;
    if (jsonData) {
        strJson = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    } else {

    }

    return strJson;
}

在修改阵列和转换的同时,我可以产生类似的崩溃。

* thread #9, stop reason = EXC_BAD_ACCESS (code=1, address=0x429a4beb8)
frame #0: 0x00000001833e8430 libobjc.A.dylib`objc_msgSend + 16
frame #1: 0x000000018404c16c CoreFoundation`-[__NSCFNumber floatValue] + 24
frame #2: 0x0000000184c0588c Foundation`+[_NSJSONReader validForJSON:depth:allowFragments:] + 704
frame #3: 0x0000000184c05a28 Foundation`__51+[_NSJSONReader validForJSON:depth:allowFragments:]_block_invoke.56 + 68
frame #4: 0x000000018405f988 CoreFoundation`-[__NSArrayM enumerateObjectsWithOptions:usingBlock:] + 216
frame #5: 0x0000000184c0571c Foundation`+[_NSJSONReader validForJSON:depth:allowFragments:] + 336
frame #6: 0x0000000184c05980 Foundation`__51+[_NSJSONReader validForJSON:depth:allowFragments:]_block_invoke + 116
frame #7: 0x00000001840a8edc CoreFoundation`-[__NSDictionaryI enumerateKeysAndObjectsWithOptions:usingBlock:] + 160
frame #8: 0x0000000184c05694 Foundation`+[_NSJSONReader validForJSON:depth:allowFragments:] + 200
frame #9: 0x0000000100d252a4 TestDeviceSensorInfo`+[MyJsonWrapper convertToJsonDataWithDictionary:](self=MyJsonWrapper, _cmd="convertToJsonDataWithDictionary:", data=0x00000001c00efe80) at MyJsonWrapper.m:18
frame #10: 0x0000000100d253b8 TestDeviceSensorInfo`+[MyJsonWrapper convertToJsonStringWithDictionary:](self=MyJsonWrapper, _cmd="convertToJsonStringWithDictionary:", data=0x00000001c00efe80) at MyJsonWrapper.m:29
frame #11: 0x0000000100d26494 TestDeviceSensorInfo`-[Caller jsonConvert:](self=0x00000001c4015f70, _cmd="jsonConvert:", dict=0x00000001c046be40) at Caller.m:127
frame #12: 0x0000000184b76860 Foundation`__NSThread__start__ + 996
frame #13: 0x0000000183db031c libsystem_pthread.dylib`_pthread_body + 308
frame #14: 0x0000000183db01e8 libsystem_pthread.dylib`_pthread_start + 312
frame #15: 0x0000000183daec28 libsystem_pthread.dylib`thread_start + 4

我已尝试将NAN NSNumber放入数组并且无法生成SIGTRAP __NSCFNumber objCType崩溃,NSNumberNSValue子类,可能是可能的原因?数组是否包含特定索引处的NSNumber,并且突然被字符串替换,希望有人可以对此问题有所了解。

这是一个简化的示例地图日志,从input_3数组和input_4数组中删除了大量条目。

2017-12-02 02:08:57.220715+0800 TestDeviceSensorInfo[4231:796162] map is {
    "input_3" =     (
        "-0.03070068",
        "-0.007385254",
        "-0.9919739",
        "0.9924763",
        "-0.02973938",
        "-0.009475708",
        "-0.9926758",
        "0.9931664",
        "-0.02876282",
        "-0.0100708"
    );
    "input_3_count" = 800;
    "input_4" =     (
        "0.00317289",
        "0.02013882",
        "0.006314355",
        "0.02134269",
        "0.003169694",
        "0.02014761",
        "0.005249357",
        "0.02106013",
        "0.004237622",
        "0.02014122"
    );
    "input_4_count" = 800;
    "input_type_num" = 2;
    "time" = 1512187737;
}

0 个答案:

没有答案