2

I've been working with this code for a couple of days trying to determine the cause of some memory leaks. Pretty much every line after the "for loop" is generating memory leak percentages in the leak performance tool.

I've been trying a variety of permutations to get it to work.

This is the code I'm running now to try and get to the bottom of it:

-(void)populateArrays
{
    NSArray *arrTempSorted;
    [arrContacts removeAllObjects];
    [arrZzoneContacts removeAllObjects];
    [arrNormalContacts removeAllObjects];

    for( int i = 0 ; i < 200 ; i++ )
    {
        [arrZzoneContacts addObject:[[[NSArray alloc] initWithObjects: [NSNumber numberWithInt:1], @"Composite Name", @"YES", @"first name", @"last name", @"first", nil] autorelease]];
        [arrNormalContacts insertObject:[[[NSArray alloc] initWithObjects: [NSNumber numberWithInt:1], @"Composite Name", @"YES", @"first name", @"last name", @"first", nil] autorelease] atIndex:[arrNormalContacts count]];
        [arrContacts addObject:[[[NSArray alloc] initWithObjects: [NSNumber numberWithInt:1], @"Composite Name", @"YES", @"first name", @"last name", @"first", nil] autorelease]];
    }

    arrTempSorted = [arrContacts sortedArrayUsingFunction:order context:NULL];
    arrContacts = [arrTempSorted mutableCopy];

    arrTempSorted = [arrZzoneContacts sortedArrayUsingFunction:order context:NULL];
    arrZzoneContacts = [arrTempSorted mutableCopy];

    arrTempSorted = [arrNormalContacts sortedArrayUsingFunction:order context:NULL];
    arrNormalContacts = [arrTempSorted mutableCopy];

    //[arrTempSorted release];

}

The arrays and function are in the appDelegate.h file. The arrays are initialized like so:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{    
    //Global variables
    arrContacts = [[NSMutableArray alloc] init];
    arrZzoneContacts = [[NSMutableArray alloc] init];
    arrNormalContacts = [[NSMutableArray alloc] init];

    // Override point for customization after application launch.
    [window addSubview:tabBarController.view];
    [self.window makeKeyAndVisible];

    return YES;
}

I've tried the method of declaring an array in didFinishLaunchingWithOptions then assigning it to the arrContacts (for example) and then releasing it but I get "bad access" errors when we get to the populateArrays method.

Any insight would be greatly appreciated and please let me know if I can provide greater detail

2 Answers 2

3

You are allocating memory for arrContacts, arrZzoneContacts, and arrNormalContacts in your second code excerpt. Then, after your for loop, you are allocating more memory for different instances of NSMutableArray by copying arrTempSorted. When you assign those new instances to your arrContacts, arrZzoneContacts, and arrNormalContacts variables, you are losing your reference to the memory you originally allocated for them, so you no longer have the opportunity to release it, hence the leak.

When you copy something, you are creating a new instance. If all you are doing with those variables is storing a copy, then you don't need to allocate the instance in the first place, copy does it for you. If you do actually need that original instance, then release it before reassigning to those variables.

For more information, see the Memory Management Programming Guide.

2

As Jim mentioned, you're overwriting your arrContacts array (and the other arrays) which is causing the leak.

As a side note though, you can avoid this problem entirely because your arrContacts (and other arrays) are already mutable. The -sortedArrayUsingFunction:context method is part of the NSArray class and it returns a non-mutable array with the results. However, NSMutableArray has a method called -sortUsingFunction:context which will do the exact same sorting but it will do it in place and just update the arrContacts array itself. No need to copy to and from other arrays.

2
  • Hi- Thanks so much for the prompt and informative responses! I'm currently experiencing a problem with the leak instrument crashing my application so I can't confirm success or failure at this point. I was experiencing some Bad Access errors when I first tried to implement these solutions. I'll write back when I have ironed out these issues and let you know what my results were.
    – Mark
    Mar 7, 2011 at 22:49
  • Follow up: I successfully resolved the leak using the sortUsingFunction:context method of NSMutableArray. Thanks again for your help!! The bad access was caused by a release being called downt he line that was unnecessary once I had fixed the leak.
    – Mark
    Mar 14, 2011 at 18:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.