Search code examples
iosmemory-leaksuipickerviewautomatic-ref-counting

Xcode 4.2.1: UIPickerView causing memory leak, using ARC


For one of my last school projects, I am creating an iPad/iPhone application. For some days now I've been working on an issue with a certain memory leak. My application starts out on a specific view-controller (VCMainStatistics_iPad). From there, I push another view-controller (VCSocialMedia_iPad). Afterwards, I go back to the first view-controller.

When I repeat this sequence, I notice (by using Instruments - Activity Monitor) that the memory usage of the app keeps increasing. By disabling parts of the code, I eventually found out it has something to do with the pickerView. This code gives no leaks:

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return 0;
}

However, when I increase the number of rows, leaks start emerging (roughly 0.07 MB per row). Obviously, this is why I believe the pickerView is the cause of the leaks. I've been trying to remove the subviews from the pickerView before deallocation, setting pickerView to nil, and lots of other things... nothing fixes the issue. To hopefully make things a bit clearer, I'll post some more code.

The header file:

#import "UniversalViewController.h"

#define DATATYPE_SOCIALMEDIA 0

@interface VCSocialMedia_iPad : UniversalViewController <UIPickerViewDataSource, UIPickerViewDelegate>
{
    NSArray *lMediaTypes;
    NSMutableArray *lMediaData;

    __weak IBOutlet UIPickerView *pkSocialMedia;
    __weak IBOutlet UILabel *lblGraph;
}

@end

PickerView delegate methods:

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    // Get key of requested row
    NSString *title = [[lMediaTypes objectAtIndex:row] capitalizedString];

    // Capitalize first letter
    title = [title capitalizedString];

    // Return
    return title;
}



- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    // Make or clear data lists
    if( lGraphDayDataX[iSelectedServerIndex][DATATYPE_SOCIALMEDIA] == nil ){
        lGraphDayDataX[iSelectedServerIndex][DATATYPE_SOCIALMEDIA] = [[NSMutableArray alloc] init];
    }
    else{
        [lGraphDayDataX[iSelectedServerIndex][DATATYPE_SOCIALMEDIA] removeAllObjects];
    }

    if( lGraphDayDataY[iSelectedServerIndex][DATATYPE_SOCIALMEDIA] == nil ){
        lGraphDayDataY[iSelectedServerIndex][DATATYPE_SOCIALMEDIA] = [[NSMutableArray alloc] init];
    }
    else{
        [lGraphDayDataY[iSelectedServerIndex][DATATYPE_SOCIALMEDIA] removeAllObjects];
    }

    // Get required key
    NSString *dictKey = [lMediaTypes objectAtIndex:row];

    if( [dictKey isEqualToString:@"total_views"] ){
        return;
    }

    // Adjust graph label
    lblGraph.text = [NSString stringWithFormat:@"Stats from %@", dictKey];

    // Get data of selected row
    NSArray *mediaData = [lMediaData objectAtIndex:row];

    // Add each day to data lists: inversed order
    for( int day = [mediaData count]-1; day >= 0; day-- ){
        NSDictionary *dayData = [mediaData objectAtIndex:day];
        dictKey = @"wpsd_trends_date";

        NSString *date = [dayData objectForKey:dictKey];
        // Remove 00:00:00
        date = [date stringByReplacingOccurrencesOfString:@" 00:00:00" withString:@""];
        [lGraphDayDataX[iSelectedServerIndex][DATATYPE_SOCIALMEDIA] addObject:date];

        dictKey = @"wpsd_trends_stats";
        NSString *stats = [dayData objectForKey:dictKey];
        [lGraphDayDataY[iSelectedServerIndex][DATATYPE_SOCIALMEDIA] addObject:stats];
    }

    // Update the graphs
    [self updateGlobalScreen];
}

PickerView datasource methods:

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}



- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return [lMediaTypes count];
}

Deallocation:

- (void)dealloc
{
    pkSocialMedia = nil;
    lblGraph = nil;

    lMediaData = nil;
    lMediaTypes = nil;
}

I only recently converted the project to Objective-C ARC, so there is a good chance this issue has something to do with my lack of experience with the concept. Apart from that, this is also my first Xcode project ever. Hopefully, someone here can help out: please let me know if I need to post more code to clarify things.

Thanks in advance!


Solution

  • Never found the solution itself, so I used a workaround: by replacing the NSPickerView with a NSTableView component, the leak did not occur anymore. For everyone who noticed the issue and tried to find a solution: thank you for trying!