3

New to iOS and trying my best to get to grips with memory management and Instruments.

My problem is I'm getting weird memory losses when adding/removing a pickerview. I've set up a very basic example consisting of two buttons: one creates and shows the picker. The other one removes it from the main view.

Analyzer comes up clean and Instruments doesn't report any obvious leaks. BUT - heap memory continues to grow when I repeat this "show and hide" operation.

Here's the code. I'm on XCode 4.5, ARC enabled:

My ViewController.h:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <UIPickerViewDataSource,UIPickerViewDelegate>

@property (nonatomic,strong) UIPickerView *myPickerView;

- (IBAction)pickerButtonPressed:(id)sender;
- (IBAction)freeButtonPressed:(id)sender;

@end

ViewController.m:

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)pickerButtonPressed:(id)sender {

    NSLog(@"Tap");

    // create picker view on first button tap
    UIPickerView* newPicker = [[UIPickerView alloc] initWithFrame:CGRectMake(20, 20,160, 160)];

    newPicker.dataSource = self;
    newPicker.delegate = self;
    newPicker.showsSelectionIndicator = YES;
    self.myPickerView = newPicker;

    [self.view addSubview:newPicker];
}

- (IBAction)freeButtonPressed:(id)sender {

    NSLog(@"Free");

    // remove from view and release
    [self.myPickerView removeFromSuperview];

    self.myPickerView = nil;
}

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{

    return 5;
}

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{

    return 2;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return @"Meh";
}

@end

Objects List on Instruments shows UIPickerView come and go nicely with every iteration, so it looks like it's being deallocated correctly. Well, not quite. This is a sample from the heapshot:

enter image description here (sorry I'm not allowed to post images yet)

Now, if I'm actually releasing the pickerview correctly (removing from view and setting its reference to nil) where do those allocations come from?

On a side note - that UIPickerTableViewTitledCell thing on the stack trace strikes me as suspect because some people have had similar problems with leaking pickerviews involving that (see this one and this one), but I can't make head or tails of what I'm supposed to do with this. Not sure if they're actually related to my issue but with any luck they might point in the right direction.

Any tips?

Community
  • 1
  • 1
MutenFuzz
  • 135
  • 1
  • 9

0 Answers0