11

I appear to have a leaky UIDatePicker. I have noted memory leaks for setting the datePickerMode to show date and time. But, I'm only trying to display date. I'm fairly certain this is a framework leak because when I comment out several UIDatePicker property assignments, the leaks go away. This code leaks 1 CGColor object, and 1 UIDeviceRGBColor object:

datePicker = [[UIDatePicker alloc] initWithFrame:[self detailViewRect]];


datePicker.datePickerMode = UIDatePickerModeDate; //leaks CGColor when setting this
datePicker.hidden = NO;
datePicker.timeZone = [NSTimeZone systemTimeZone]; //leaks UIDeviceRGBColor
datePicker.maximumDate = [NSDate date];//leaks UIDeviceRGBColor
datePicker.autoresizingMask = UIViewAutoresizingFlexibleWidth;

By commenting the lines Instruments reported as offending, I get no leaks:

datePicker = [[UIDatePicker alloc] initWithFrame:[self detailViewRect]];

//datePicker.datePickerMode = UIDatePickerModeDate; //leaks CGColor when setting this
datePicker.hidden = NO;
//datePicker.timeZone = [NSTimeZone systemTimeZone]; //leaks UIDeviceRGBColor
//datePicker.maximumDate = [NSDate date];//leaks UIDeviceRGBColor
datePicker.autoresizingMask = UIViewAutoresizingFlexibleWidth;

Without at least date mode set, the UIDatePicker is worthless. Does anyone have a workaround?

stephen
  • 1,039
  • 14
  • 31
  • 1
    Did you check the leak on the device? Or on the simulator only? Sometimes simulator has memory leak bug that does not exist on device. – barley Oct 11 '12 at 01:08
  • Regarding workaround, how bad is the leak? – Carl Veazey Oct 11 '12 at 04:41
  • @barley Thanks for the reminder. No leaks on the device. I'm not using ARC. Instruments reports 50B every time the date picker is deallocated. I don't expect users to use this with high frequency. – stephen Oct 11 '12 at 23:25
  • Having the same issue. I have ARC enabled. Not a big deal if the users aren't going to be using the date picker that much. – ninjaneer Dec 15 '12 at 04:46

1 Answers1

2

This approach worked for me:

Create the DatePicker in xib instead of the programmatic approach. Then, on the Attributes Inspector, set Mode to Date.

yoninja
  • 1,952
  • 2
  • 31
  • 39
  • I presume this means you were having the problem before; and that you solved the problem by doing this. If so, what environment were you working in? And what did you try that caused the problem previously? – stephen Dec 21 '12 at 05:13
  • Was building for iPad iOS6. Xcode 4.5.1 with iOS6.0. Initially, I created the datePicker programatically--meaning no xib file-- then I was setting the mode this way: [_datePicker setDatePickerMode:UIDatePickerModeDate]; I got memory leak when I check in Instruments. I reimplemented the date picker with an xib and, instead of setting the mode like before, I set it in the xib. When I checked it in Instruments there were no leaks. – yoninja Dec 21 '12 at 08:39
  • 1
    I too can confirm that if you set these attributes in IB (instead of programmatically), the `UIDatePicker` doesn't show a leak in Instruments. However, the `UIDatePicker` will leak if you set said attributes later via code (even if the `UIDatePicker` is created in IB). This is clearly a leak in the framework code, and unfortunately, until Apple fixes it, there isn't a good workaround, other than accepting that it's going to leak... : / – JRG-Developer Jan 14 '13 at 07:51
  • 1
    The leak is still here in iOS 6.1.4. I have tried also to set the date with setDate:animated: before and after the date picker is displayed but doesn't change nothing... Anyway I'm experiencing the leak when the picker is removed from the view and not when is created. – ggould75 May 22 '13 at 07:22
  • I can also confirm this. Both CGColor and UIDeviceRGBColor leak. – nmdias Aug 12 '13 at 10:37
  • Hi, does anyone know if apple would reject an app because of this? – khanh.tran.vinh Aug 31 '13 at 18:16
  • 1
    I believe this is won't be a reason for rejection. – yoninja Sep 02 '13 at 03:09
  • Apple didnt reject my app, that is setup programtically as above and to me is clearly a bug in the IOS. This used to work well. This isnt a problem with just the datepicker either. It seems to be any view that I have popping up with a date, time, listview selection etc. Itgs really a mess for me. I cant rewrite the whole app and figure out how to put all of the different functions in an xib. Right now I am looking at another section of the code that has a list view but there is a block overlapping the list or the list isnt wide enough. I havent a clue whats wrong as this used to work well. – Mark Worsnop Nov 12 '13 at 20:52
  • I wound up basically finding the places where the initWithFrame:CGRectMake was called and changing those calls width to 300. I am still testing, but I think that fixed the problems. – Mark Worsnop Nov 12 '13 at 22:55
  • 5
    I'm having the same problem as the original poster in iOS 8. This solution of using an xib still causes a memory leak. Manually setting `UIDatePickerModeDate` or using the Attributes Inspector both call `-[_UIDatePickerMode _yearlessYearForMonth:]` which leaks. – Jonathan Beebe Feb 04 '15 at 13:31