从 NSJSONSerialization 处理基础对象时内存泄漏

Memory leak while handling Foundation Object from NSJSONSerialization

I'm struggling to fix a memory leak in a helper function I have made. The helper function takes the result of

+ (id)JSONObjectWithData:(NSData *)data options:(NSJSONReadingOptions)opt error:(NSError * _Nullable *)error 

and converts all the leaf elements into NSStrings if they are NSNumbers.

Here is the method:

-(NSArray *) stringisizeObjects:(NSArray *)inputArray{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    NSMutableArray *mutable = [[NSMutableArray alloc] initWithCapacity:[inputArray count]];

    for (int i = 0; i < [inputArray count]; i++) {
        NSArray *keys = [inputArray[i] allKeys];

        NSMutableDictionary *addDictionary = [[NSMutableDictionary alloc] initWithCapacity:[keys count]];

        for (int j = 0; j < [keys count]; j++) {

            id theObject = [[inputArray[i] objectForKey:keys[j]]autorelease];

            if ([theObject isKindOfClass:[NSNumber class]]) {

                [addDictionary setObject:[theObject stringValue] forKey:keys[j]];
                [theObject release];

            }else if ([theObject isKindOfClass:[NSString class]]){
                [addDictionary setObject:[inputArray[i] objectForKey:keys[j]] forKey:keys[j]];
            }

        }
        [mutable addObject:addDictionary];
    }
    NSArray *returnArray = [mutable copy];

    [mutable removeAllObjects];
    [mutable release];
    [pool drain];
    return returnArray;
}

Here is how I get the input array.

id parsedThingy = [NSJSONSerialization JSONObjectWithData:resultJSONData options:1 error:&jsonDecodeError];

Before I can pass the result to my stringisize method I must ensure that I have an NSArray of NSDictionaries with matching keys.

NSArray *resultArray = [self stringisizeObjects:parsedThingy];

The X-Code memory leaks tool has pointed me to this method as the cause of my problem.

Instruments showing leaks

As you can see I have tried wrapping things in autorelease pools, autoreleasing and releasing. I just don't see any way forward here.

This is a non ARC project that runs 24/7.

Edit: I took the advice from Droppy and tried to re-write the method using mutableCopy. The leak is still there. At this point my only work around maybe to change the source of the JSON to send only strings. :(

-(NSArray *) stringisizeObjects2:(NSArray *)inputArray{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    NSMutableArray *mutableArray = [inputArray mutableCopy];

    for (int i = 0; i < [mutableArray count]; i++) {
        NSMutableDictionary *mutableDict = [mutableArray[i] mutableCopy];
        NSArray *keys = [mutableDict allKeys];

        for (int j = 0; j < [keys count]; j++) {
            if ([[mutableDict objectForKey:keys[j]] isKindOfClass:[NSNumber class]]) {
                NSString *stringValue = [[mutableDict objectForKey:keys[j]] stringValue];

                [mutableDict removeObjectForKey:keys[j]];
                [mutableDict setObject:stringValue  forKey:keys[j]];
            }
        }
        mutableArray[i] = [mutableDict copy];
        [mutableDict release];
    }

    NSArray *returnArray = [mutableArray copy];

    [mutableArray release];
    [pool drain];
    return returnArray;
}

问题:

  1. addDictionary 调用了 alloc 但未调用 releaseautorelease
  2. returnArray = [可变副本]; // 确实增加了 retainCount +1,这里需要 autorelease
  3. id theObject = [inputArray[i] objectForKey:keys[j]]; // 不需要为你不拥有的对象自动释放或释放
  4. 在此处添加 NSAutoreleasePool 到顶部底部什么都不做

解决方案:

-(NSArray *) stringisizeObjects:(NSArray *)inputArray{
    NSMutableArray *mutable = [[NSMutableArray alloc] initWithCapacity:[inputArray count]];

    for (int i = 0; i < [inputArray count]; i++) {
        NSArray *keys = [inputArray[i] allKeys];

        NSMutableDictionary *addDictionary = [[NSMutableDictionary alloc] initWithCapacity:[keys count]];

        for (int j = 0; j < [keys count]; j++) {

            id theObject = [inputArray[i] objectForKey:keys[j]]; // not need autorelease

            if ([theObject isKindOfClass:[NSNumber class]]) {

                [addDictionary setObject:[theObject stringValue] forKey:keys[j]];
                //[theObject release]; // not need release value here

            }else if ([theObject isKindOfClass:[NSString class]]){
                [addDictionary setObject:[inputArray[i] objectForKey:keys[j]] forKey:keys[j]];
            }

        }
        [mutable addObject:addDictionary];
        [addDictionary release]; // release after not use
    }
    NSArray *returnArray = [[[NSArray alloc] initWithArray:mutable] autorelease]; // auto release for return value

    [mutable removeAllObjects];
    [mutable release];
    return returnArray;
}