简体   繁体   English

使用ARC处理NSError-泄漏

[英]handle NSError using ARC - leak

- (BOOL)parserJSONString:(NSString *)jsonString error:(NSError **)anError {
   //some data getting
   //error handle

    NSString *description = @"phone number couldn't be using";
    NSString *recoverySuggestion = @"Please provide an other phone number.";
    NSInteger errorCode = -1;
    NSArray *keys = [NSArray arrayWithObjects: NSLocalizedDescriptionKey, NSLocalizedRecoverySuggestionErrorKey, nil];
    NSArray *values = [NSArray arrayWithObjects:description, recoverySuggestion, nil];
    NSDictionary *userDict = [NSDictionary dictionaryWithObjects:values forKeys:keys];
    *anError = [[NSError alloc] initWithDomain:@"my domain" code:errorCode userInfo:userDict];
    return NO;
}

*anError = [[NSError alloc] initWithDomain:@"my domain" code:errorCode userInfo:userDict]; compiler give next leak warning "Potential null dereference. According to coding standards in 'Creating and Returning NSError Objects' the parameter '' may be null" 编译器给出下一个泄漏警告“潜在的空取消引用。根据'创建和返回NSError对象'中的编码标准,参数”可能为空”
How to fix this? 如何解决这个问题?

You need to first check whether anError is nil or NULL : 您需要首先检查anErrornil还是NULL

if (anError) {
    *anError = [[NSError alloc] initWithDomain:@"my domain" code:errorCode userInfo:userDict];
}

This is not actually a leak warning, but a potential dereference of a null pointer. 这实际上不是泄漏警告,而是对空指针的潜在取消引用。 The compiler is complaining about the line 编译器抱怨这行

*anError = [[NSError alloc] initWithDomain:@"my domain" code:errorCode userInfo:userDict];

You assign to the location being pointed to by anError without checking, whether anError is actually the null pointer (which is allowed "according to the coding standard", and may happen, if the caller is not interested in detailed error information). 您指定的位置被指向anError没有检查,是否anError实际上是空指针(这是允许“根据编码标准”,并可能发生,如果主叫方不感兴趣的详细的错误信息)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM