简体   繁体   中英

Memory Leaks - Objective-C

Can anyone help point out memory leaks? I'm getting a bunch within this method and I'm not sure exactly how to fix it.

- (NSMutableArray *)getTop5AndOtherKeysAndValuesFromDictionary:(NSMutableDictionary *)dict {
    NSLog(@"get top 5");
    int sumOfAllValues = 0;

    NSMutableArray *arr = [[[NSMutableArray alloc] init] retain];

    for(NSString *key in dict){
        NSString *value = [[dict objectForKey:key] retain];
        [arr addObject:value];
        sumOfAllValues += [value intValue];
    }

    //sort values
    NSArray *sorted = [[arr sortedArrayUsingFunction:sort context:NULL] retain];

    [arr release];

    //top 5 values
    int sumOfTop5 = 0;
    NSMutableArray *top5 = [[[NSMutableArray alloc] init] retain];
    for(int i = 0; i < 5; i++) {
        int proposedIndex = [sorted count] - 1 - i;
        if(proposedIndex >= 0) {
            [top5 addObject:[sorted objectAtIndex:([sorted count] - i - 1)]];
            sumOfTop5 += [[sorted objectAtIndex:([sorted count] - i - 1)] intValue];
        }
    }

    [sorted release];

    //copy of all keys
    NSMutableArray *copyOfKeys = [[[NSMutableArray alloc] init] retain];
    for(NSString *key in dict) {
        [copyOfKeys addObject:key];
    }


    //copy of top 5 values
    NSMutableArray *copyOfTop5 = [[[NSMutableArray alloc] init] retain];
    for(int i = 0; i < [top5 count]; i++) {
        [copyOfTop5 addObject:[top5 objectAtIndex:i]];
    }


    //get keys with top 5 values
    NSMutableArray *outputKeys = [[[NSMutableArray alloc] init] retain];
    for(int i = 0; i < [top5 count]; i++) {
        NSString *targetValue = [top5 objectAtIndex:i];
        for(int j = 0; j < [copyOfKeys count]; j++) {
            NSString *key = [copyOfKeys objectAtIndex:j];
            NSString *val = [dict objectForKey:key];
            if([val isEqualToString:targetValue]) {
                [outputKeys addObject:key];
                [copyOfKeys removeObjectAtIndex:j];
                break;
            }
        }
    }


    [outputKeys addObject:@"Other"];
    [top5 addObject:[[NSString stringWithFormat:@"%d",(sumOfAllValues - sumOfTop5)] retain]];


    NSMutableArray *output = [[NSMutableArray alloc] init];
    [output addObject:outputKeys];
    [output addObject:top5];

    NSMutableArray *percents = [[NSMutableArray alloc] init];
    int sum = sumOfAllValues;
    float leftOverSum = sum * 1.0f;

    int count = [top5 count];
    float val1, val2, val3, val4, val5;
    if(count >= 1)
        val1 = ([[top5 objectAtIndex:0] intValue] * 1.0f)/sum;
    else
        val1 = 0.0f;

    if(count >=2)
        val2 = ([[top5 objectAtIndex:1] intValue] * 1.0f)/sum;
    else
        val2 = 0.0f;

    if(count >= 3)
        val3 = ([[top5 objectAtIndex:2] intValue] * 1.0f)/sum;
    else
        val3 = 0.0f;

    if(count >= 4)
        val4 = ([[top5 objectAtIndex:3] intValue] * 1.0f)/sum;
    else
        val4 = 0.0f;

    if(count >=5)
        val5 = ([[top5 objectAtIndex:4] intValue] * 1.0f)/sum;
    else
        val5 = 0.0f;


    if(val1 >= .00001f) {
        NSMutableArray *a1 = [[NSMutableArray alloc] init];
        [a1 addObject:[outputKeys objectAtIndex:0]];
        [a1 addObject:[top5 objectAtIndex:0]];
        [a1 addObject:[NSString stringWithFormat:@"%.01f",(val1*100)]];
        [percents addObject:a1];
        leftOverSum -= ([[top5 objectAtIndex:0] intValue] * 1.0f);
    }
    if(val2 >= .00001f) {
        NSMutableArray *a2 = [[NSMutableArray alloc] init];
        [a2 addObject:[outputKeys objectAtIndex:1]];
        [a2 addObject:[top5 objectAtIndex:1]];
        [a2 addObject:[NSString stringWithFormat:@"%.01f",(val2*100)]];
        [percents addObject:a2];        
        leftOverSum -= ([[top5 objectAtIndex:1] intValue] * 1.0f);
    }
    if(val3 >= .00001f) {
        NSMutableArray *a3 = [[NSMutableArray alloc] init];
        [a3 addObject:[outputKeys objectAtIndex:2]];
        [a3 addObject:[top5 objectAtIndex:2]];
        [a3 addObject:[NSString stringWithFormat:@"%.01f",(val3*100)]];
        [percents addObject:a3];        
        leftOverSum -= ([[top5 objectAtIndex:2] intValue] * 1.0f);
    }
    if(val4 >= .00001f) {
        NSMutableArray *a4 = [[NSMutableArray alloc] init];
        [a4 addObject:[outputKeys objectAtIndex:3]];
        [a4 addObject:[top5 objectAtIndex:3]];
        [a4 addObject:[NSString stringWithFormat:@"%.01f",(val4*100)]];
        [percents addObject:a4];        
        leftOverSum -= ([[top5 objectAtIndex:3] intValue] * 1.0f);
    }
    if(val5 >= .00001f) {
        NSMutableArray *a5 = [[NSMutableArray alloc] init];
        [a5 addObject:[outputKeys objectAtIndex:4]];
        [a5 addObject:[top5 objectAtIndex:4]];
        [a5 addObject:[NSString stringWithFormat:@"%.01f",(val5*100)]];
        [percents addObject:a5];        
        leftOverSum -= ([[top5 objectAtIndex:4] intValue] * 1.0f);
    }

    float valOther = (leftOverSum/sum);
    if(valOther >= .00001f) {
        NSMutableArray *a6 = [[NSMutableArray alloc] init];
        [a6 addObject:[outputKeys objectAtIndex:5]];
        [a6 addObject:[top5 objectAtIndex:5]];
        [a6 addObject:[NSString stringWithFormat:@"%.01f",(valOther*100)]];
        [percents addObject:a6];    
    }

    [output addObject:percents];

    NSLog(@"mu - a");
    //[arr release];
    NSLog(@"mu - b");
    //[copyOfKeys release];
    NSLog(@"mu - c");
    //[copyOfTop5 release];
    NSLog(@"mu - c");
    //[outputKeys release];
    //[top5 release];
    //[percents release];

    return output;  
}

1.

NSMutableArray *arr = [[[NSMutableArray alloc] init] retain];

There are only few ways to explicitly increment 1 in objective-c's memory management such as alloc, retain, copy, attain, new. If you use any of these, you must release in the future. You used "alloc" and "retain" to the same variable "arr".

2.

NSString *value = [[dict objectForKey:key] retain];
[arr addObject:value];
sumOfAllValues += [value intValue];

You don't need to "retain" there. Also after you add value to an array "arr", "arr" will release its ALL elements when "arr" is released by you. So if you want to keep "retain" it, you have to [value release] in the last line of this for loop.

3.

NSArray *sorted = [[arr sortedArrayUsingFunction:sort context:NULL] retain];

Again, you don't need to "retain." If you only write [arr sortedArrayUsingFunction:sort context:NULL], then it will automatically gives an object with autorelease option. So you don't have to care about "release" in the future.

All others in your code have similar problem to these first three. Just make sure

1. If you used one of "alloc", "retain", "attain", "copy", "new", "mutableCopy" and so on, you must release in the future.

2. NSArray and NSMutableArray will release their elements when you release them. So after you add any object and don't need to refer by that variable, then it's better to release after adding to an array. Other collectors or containers work in the same way.

3. Similarly, if you add any UIView instance as a subview to superview, then that superview will care its subviews' memory management. In other words, if you release superview, its subviews will automatically be released.

Edit:

4. "retain" is used to increment an internal counter of an object. Usually, it's used in a setter method to give an ownership from an old variable to a new variable. So, you almost never use "retain" when you create a new instance.

Please, correct me if I'm wrong!

I know this won't get any up votes as it doesn't specifically answer your question. Nonetheless it will be the best piece of advice you will get. Get yourself a great profiler for Objective-C on the mac. You will never post a question like this again on SO and it will enlighten you to all the things wrong within your code. I am not being tongue in cheek. Seriously no one here should try to analyze your code for memory leaks. There are people that dedicate their careers on developing software to do this for you.

Obviously there are bound to be leaks in your code.Why do you keep on retaining you Mutable array every time you initialize it?

You should try to find out your memory leak with the help of CLANG(static analyzer).You can do it by typing cmd+shift+A in your code window.

Cheers

start with the static analyzer - there's a lot in there. the static analyzer with catch many of them.

It would really help if you were to mention which objects you are leaking. In addition I suspect that a "build and analyze" run would walk you through many, if not all, of them.

For example [[[NSMutableArray alloc] init] retain] gives you an object you own two retains on. That doesn't seem to be what you are expecting it to do.

See http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmRules.html#//apple_ref/doc/uid/20000994-BAJHFBGH

You take ownership of an object if you create it using a method whose name begins with “alloc” or “new” or contains “copy” (for example, alloc, newObject, or mutableCopy), or if you send it a retain message.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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