4

I am seeing memory leaks in UIDatePicker when used in a popover on an iPad running IOS 8.3. I'm getting approx 5K in multiple memory leaks every time the date picker is popped up and then dismissed. The leaked object is NSDateComponents, and the responsible frame is [_UIDatePickerMode _yearlessYearForMonth:].

I have written a simple test app to demonstrate the problem (https://github.com/david-ape/datepickertest/). I've included both a UIPopoverController option and a UIPopoverPresentationController option, but it doesn't seem to matter which is used.

Am I doing something wrong, or is there a workaround, or do I need to wait for a fix from Apple? If the latter, then can anyone suggest a third party control that I could use in place of UIDatePicker?

Below is the code I'm using to pop up the date pickers.

Header file

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <UIPopoverControllerDelegate,
                                              UIPopoverPresentationControllerDelegate>


@end

Implementation file

#import "ViewController.h"

@interface ViewController ()

@property (nonatomic, strong) UIPopoverController *ios7Popover;

- (IBAction)datePickerPopupIOS7:(UIButton *)sender;
- (IBAction)datePickerPopupIOS8:(UIButton *)sender;

@end

@implementation ViewController

// helper - returns a view controller containing a date picker for use in a
// popup
+ (UIViewController *)buildDatePickerViewController
{
    CGRect frame = CGRectMake(0, 0, 350, 216);

    UIViewController *viewController = [[UIViewController alloc]init];
    viewController.preferredContentSize = frame.size;
    UIDatePicker *datepicker = [[UIDatePicker alloc]initWithFrame:frame];
    datepicker.datePickerMode = UIDatePickerModeDate;
    datepicker.hidden = NO;
    datepicker.date = [NSDate date];
    [viewController.view addSubview:datepicker];
    return viewController;
}

// popup date picker using UIPopoverController (IOS7 compatible)
- (IBAction)datePickerPopupIOS7:(UIButton *)sender
{
    UIViewController *viewController = [ViewController buildDatePickerViewController];

    self.ios7Popover = [[UIPopoverController alloc]initWithContentViewController:viewController];
    self.ios7Popover.delegate = self;
    [self.ios7Popover presentPopoverFromRect:sender.frame
                                      inView:self.view
                    permittedArrowDirections:(UIPopoverArrowDirectionUp|UIPopoverArrowDirectionDown| UIPopoverArrowDirectionLeft|UIPopoverArrowDirectionRight)
                                    animated:YES];
}

// popup date picker using UIPopoverPresentationController (IOS8 or later required)
// Thanks to http://stackoverflow.com/a/26944036/1764243 for how to do this
- (IBAction)datePickerPopupIOS8:(UIButton *)sender
{
    if ([UIPopoverPresentationController class])
    {
        UIViewController *viewController = [ViewController buildDatePickerViewController];

        UINavigationController *destNav = [[UINavigationController alloc] initWithRootViewController:viewController];
        destNav.modalPresentationStyle = UIModalPresentationPopover;
        UIPopoverPresentationController *popover = destNav.popoverPresentationController;
        popover.delegate = self;
        popover.sourceView = self.view;
        popover.sourceRect = [sender frame];
        destNav.navigationBarHidden = YES;
        [self presentViewController:destNav animated:YES completion:nil];
    }
    else
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Not supported"
                                                        message:@"UIPopoverPresentationController not supported in this version of IOS"
                                                       delegate:nil
                                              cancelButtonTitle:@"Ok"
                                              otherButtonTitles:nil];
        [alert show];
    }
}

#pragma mark - UIPopoverControllerDelegate methods

 - (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController
{
    self.ios7Popover = nil;
}

@end
david
  • 123
  • 1
  • 8
  • I have the same issue: http://stackoverflow.com/questions/32129875/memory-leak-in-apple-ios-library-method-uidatepickermode-yearlessyearformont?noredirect=1#comment52150698_32129875 .Seems to be bug. If there is a bug in picker, then every iOS app will suffer, hence Apple should fix it soon. Did you issued them this bug? – user996142 Aug 20 '15 at 23:15
  • Yes I have reported it. Apple have asked me to check if it is in the IOS 9 beta, but unfortunately I haven't yet had time to do this. – david Aug 21 '15 at 04:14
  • BTW, this issue reproduces only when you set picker value directly of via storyboard. I've issues apple this bug as well. – user996142 Sep 10 '15 at 00:02

0 Answers0