cybergarage/iCal4ObjC — Issue #1: NSDate+CGICalendar.m leaks
Author: sixten
Created: 2012-01-19 16:43:17+00:00
State: open

As called out by the static analyzer, the implementations of the methods in this category all leak the date formatters that they create. For example:

```
+ (id)dateWithICalendarString:(NSString *)aString
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"UTC"];
[dateFormatter setTimeZone:timeZone];
[dateFormatter setDateFormat:CGNSDATE_ICALENDAR_DATETMEFORMAT];
return [dateFormatter dateFromString:aString];
}
```

The simplest solution would be to autorelease `dateFormatter`, or to save the result in an intermediate variable so that the formatter can be released before returning:

```
+ (id)dateWithICalendarString:(NSString *)aString
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"UTC"];
[dateFormatter setTimeZone:timeZone];
[dateFormatter setDateFormat:CGNSDATE_ICALENDAR_DATETMEFORMAT];
NSString *result = [dateFormatter dateFromString:aString];
[dateFormatter release];
return result;
}
```

As I understand it, though, `NSDateFormatter`s are fairly expensive to create, so it might also be worth thinking about whether it makes sense to try to cache these somehow across invocations.