rowwingman
rowwingman

Reputation: 5581

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"
How to fix this?

Upvotes: 1

Views: 2615

Answers (2)

Dirk
Dirk

Reputation: 31063

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).

Upvotes: 6

DarkDust
DarkDust

Reputation: 92384

You need to first check whether anError is nil or NULL:

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

Upvotes: 14

Related Questions