Segev
Segev

Reputation: 19303

Memory issues because of UIDatePicker

- (void)viewDidAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    _datePicker = [[UIDatePicker alloc] init];
    _datePicker.datePickerMode = UIDatePickerModeDate;

    _datePicker.frame = CGRectMake(70, self.view.frame.size.height ,250,100);
    _datePicker.alpha = 0;
    [_datePicker addTarget:self action:@selector(saveDate:)  forControlEvents:UIControlEventEditingDidEnd];
    [self.view addSubview:_datePicker];
}

I'm trying to figure out where is my "unbounded memory growth" , I'm using ARC in my app. I have a few memory issues in my app and I'm trying to figure them out one by one.

While moving between 2 viewControllers I can clearly see a big increase in memory use, the main cause for it is the code I wrote here. What am I doing wrong, how can I release it and where?

Thanks

Upvotes: 0

Views: 417

Answers (1)

nebs
nebs

Reputation: 4989

viewDidAppear: gets called every time the view appears (even if the controller has been initialized already). So each time the view appears you are allocating a new UIDatePicker without releasing the old one.

If your datePicker is defined as a @property with "retain" then I would use

self.datePicker = [[[UIDatePicker alloc] init] autorelease];

By using self.datePicker you are calling the synthesized setter which will automatically release the old value for you.

Alternatively you can move this initialization to initWithNibName: or viewDidLoad: instead, that way it will only get called once.

Try this out and see if it helps your memory.

(I'm assuming you're not using ARC, otherwise what I said won't really help you).

Upvotes: 1

Related Questions