NSMutableDictionary在JSONModel中出现了EXC_BAD_ACCESS KERN_INVALID_ADDRESS错误。

10

我的一个应用程序在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];
    }];

}

1
请勿发布代码截图,而是复制/粘贴代码。这对我们来说更简单,如果我们想要复制您的代码以显示错误,我们必须手动编写它(无法复制/粘贴),而且阅读起来也更困难... - Larme
我已经发布了屏幕截图以标出行号,但我将编辑问题。 - stonycis
2个回答

2
因此,setObject:forKey: 可能会以两种方式引起问题:1. 如果 objectnil 或者 2. keynil。两者都可能导致您看到的崩溃。鉴于您将 object 设置为 [NSNull null],可以安全地假设是 key 给您带来了问题(位于第855行)。
从那里回退,就会发现 classNamenil。如果您仔细查看代码,会发现您的代码没有保护它。您在这里做出了一个假设,即 NSStringFromClass(几行之前)会给您返回一个有效的字符串,这意味着传递到该方法中的 value 原始值是非-nil 的。如果它是 nil,它将通过您所有的检查,包括 !property.customSetters[className],因为这将是 !nil,允许其进入 if
如果我正确阅读您的代码(有点难以测试我的假设),NSLog(@"response: %@",response); 将打印出一个 nil 响应。
尝试查看您的代码如何处理这些意外的 nil,并在评论中让我知道情况如何。

如果“response”对象为nil,JSONModel应该能够处理它。请查看JSONModel库中的堆栈跟踪方法(此处)。我无法在评论中粘贴代码(太长了)。 - stonycis
即使我手动将“response”设置为nil,它也不会触及导致崩溃的那段代码。 - stonycis
@stonycis 感谢您的回复。我会再次查看它(现在没有时间),并更新我的答案。 - Firo
1
是的,如果我强制将 className 设为 nil,那么就会出现崩溃(这是我测试的第一件事情之一),但是我无法创建一个场景来重现它,除非手动将其设置为 nil - stonycis
有一个想法...多个并发调用(来自不同的线程)reloadUserInfoWithCompletion方法是否可能会导致崩溃?(仍然无法复现) - stonycis
显示剩余2条评论

0

如果您不使用模型自定义setter,您可以使用Swizzling或Aspects库替换JSONModel __customSetValue:forProperty:

#import "JSONModel+Aspects.h"
#import "JSONModel.h"
#import "Aspects.h"

@implementation JSONModel (Aspects)

+(void)load {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        [JSONModel aspect_hookSelector:@selector(__customSetValue:forProperty:) withOptions:AspectPositionInstead usingBlock:^(id<AspectInfo> aspectInfo) {
            return NO;
        } error:NULL];
    });
}

@end

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接