views:

33

answers:

1

Hello geniuses,

I'm trying to write a simple function using Objective C that accepts an NSDate object and returns an NSDate object that contains the same date value but has removed any time components from the date.

For example if I were to pass an NSDate with a value of '2010-10-12 09:29:34' the function would return 2010-10-12 00:00:00.

The I'm using function seems to work properly. However, when I test the iPhone app I'm developing with Instruments it reports there is a memory leak in the function I'm using. Take a look at my function below. Where is there a memory leak? Is there a better way to achieve the functionality I desire?

Thanks in advance!!

-(NSDate *)dateWithOutTime:(NSDate *)datDate
{
    if (datDate == nil)
    {
        datDate = [NSDate date];
    }

    unsigned int      intFlags   = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
    NSCalendar       *calendar   = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
    NSDateComponents *components = [[[NSDateComponents alloc] init] autorelease];

     components = [calendar components:intFlags fromDate:datDate];

     return [calendar dateFromComponents:components];
}
A: 

Try this:

-(NSDate *)dateWithOutTime:(NSDate *)datDate
    if( datDate == nil ) {
        datDate = [NSDate date];
    }
    NSDateComponents* comps = [[NSCalendar currentCalendar] components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:datDate];
    return [[NSCalendar currentCalendar] dateFromComponents:comps];
}
aegzorz
Hmmmmmm, the function works. However I still detect the same memory leak when I analyze using Instruments.
CMJ Designs
That's weird, there should be no memory leaks since all calls are autoreleased. Looking at your original implementation again, there should be no leaks there either. Are you running leaks in the simulator or on a device?
aegzorz
I'm testing with both simulator and device. After some further investigation I've found that you are correct. There is no memory leak in the function you provided or the original function I was using. The leak is somewhere else in my code. Thanks for assistance.
CMJ Designs