ios - JSONModel 中的 NSMutableDictionary - EXC_BAD_ACCESS KERN_INVALID_ADDRESS

标签 ios xcode crash exc-bad-access jsonmodel

Crashlytics 在我的一个应用程序中报告了此崩溃,但无论我做什么,我都无法重现它。 这种情况发生在大约 5% 的用户身上,所以这是一个相当大的问题。 我正在发布带有崩溃报告的屏幕截图以及崩溃报告中提到的方法。 知道如何解决这个问题吗?

Crash Report

这是应用程序崩溃的地方:

#pragma mark - custom transformations
-(BOOL)__customSetValue:(id<NSObject>)value forProperty:(JSONModelClassProperty*)property
{
    if (!property.customSetters)
        property.customSetters = [NSMutableDictionary new];

    NSString *className = NSStringFromClass([JSONValueTransformer classByResolvingClusterClasses:[value class]]);

    if (!property.customSetters[className]) {
        //check for a custom property setter method
        NSString* ucfirstName = [property.name stringByReplacingCharactersInRange:NSMakeRange(0,1)
                                                                       withString:[[property.name substringToIndex:1] uppercaseString]];
        NSString* selectorName = [NSString stringWithFormat:@"set%@With%@:", ucfirstName, className];

        SEL customPropertySetter = NSSelectorFromString(selectorName);

        //check if there's a custom selector like this
        if (![self respondsToSelector: customPropertySetter]) {
            property.customSetters[className] = [NSNull null]; // this is line 855
            return NO;
        }

        //cache the custom setter selector
        property.customSetters[className] = selectorName;
    }

    if (property.customSetters[className] != [NSNull null]) {
        //call the custom setter
        //https://github.com/steipete
        SEL selector = NSSelectorFromString(property.customSetters[className]);
        ((void (*) (id, SEL, id))objc_msgSend)(self, selector, value);
        return YES;
    }

    return NO;
}

这是原始方法:

-(void)reloadUserInfoWithCompletion:(void (^) (LoginObject *response))handler andFailure:(void (^)(NSError *err))failureHandler {
    NSString *lat;
    NSString *lon;

    lat = [NSString stringWithFormat:@"%.6f",[[LocationManager sharedInstance] getPosition].coordinate.latitude];
    lon = [NSString stringWithFormat:@"%.6f",[[LocationManager sharedInstance] getPosition].coordinate.longitude];

    NSMutableDictionary *params = [NSMutableDictionary new];
    [params setObject:lat forKey:@"latitude"];
    [params setObject:lon forKey:@"longitude"];

    [[LoginHandler sharedInstance] getLoginToken:^(NSString *response) {

        NSDictionary *headers;
        if (response) {
            headers = @{@"Login-Token":response};
        }
        GETRequest *req = [GETRequest new];
        [req setCompletionHandler:^(NSString *response) {
            dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                NSLog(@"response: %@",response);
                NSError *err = nil;
                self.loginObject.userDetails = [[User alloc] initWithString:response error:&err]; // <- this is the line reported in the crash
                [self storeLoginObject];
                NSLog(@"%@",self.loginObject.userDetails);
//                [Utils updateFiltersFullAccessIfAll]; 
                dispatch_async(dispatch_get_main_queue(), ^{
                    if (handler) {
                        handler(self.loginObject);
                    }
                });
            });
        }];
        [req setFailedHandler:^(NSError *err) {
            if (failureHandler) {
                failureHandler(err);
            }
        }];
        NSLog(@"%@",params);
        [req requestWithLinkString:USER_DETAILS parameters:nil andHeaders:headers];
    }];

}

最佳答案

因此 setObject:forKey: 可能会以两种方式引起问题。 1. 如果 objectnil 或 2. keynil。两者都可能导致您看到的崩溃。鉴于您将 object 设置为 [NSNull null] 可以安全地假设它是 key 给您带来问题(在线855).

从那里往回走会发现 classNamenil。如果你看,你的代码并不能防止这种情况。您在这里假设 NSStringFromClass(之前的几行)给您返回一个有效的字符串,它假设最初传递给方法的 value 是非 。如果它是 nil,它将通过您的所有检查,包括 !property.customSetters[className],因为这将是 !nil允许它输入 if

如果我正确阅读你的代码(有点难,因为我无法测试我的任何假设)NSLog(@"response: %@",response); 会打印出一个 响应。

尝试查看您的代码如何处理这些意外的 nil 并在评论中告诉我事情进展如何。

关于ios - JSONModel 中的 NSMutableDictionary - EXC_BAD_ACCESS KERN_INVALID_ADDRESS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35012568/

相关文章:

ios - Xcode 8.2.1 & Swift 3.0.2 + Realm 2.4.x = 致命异常崩溃

ios - 应用程序在 iOS10 上崩溃,异常 "There doesn' t 似乎是路径中有效的编译 Storyboard”

iphone - 将对象添加到可变数组

ios - 如何快速将字符串转换为URL

windows-phone-8 - Windows Phone 8-打开Goog​​le Admob非页内广告后,应用程序崩溃

ios - X代码/swift 3 : How to shift storyboard elements down/up without messing up existing constraints?

ios - Xcode 7.2 更新后,可执行文件使用无效权利进行签名

iphone - 测试理解 Objective-C 与指针

jquery - iOS Safari Mobile 图像 map - 单击时区域显示框

ios - touchBegan 和 TouchEnded 重写正在影响另一个 UIViewController