开发者

why are these methods leaking NSDate objects?

First one:

+ (NSDate*)convertToUTC:(NSDate*)sourceDate
{
    NSTimeZone* currentTimeZone = [NSTimeZone localTimeZone];
    NSTimeZone* utcTimeZone = [NSTimeZone timeZoneWithAbbreviation:开发者_Python百科@"UTC"];

    NSInteger currentGMTOffset = [currentTimeZone secondsFromGMTForDate:sourceDate];
    NSInteger gmtOffset = [utcTimeZone secondsFromGMTForDate:sourceDate];
    NSTimeInterval gmtInterval = gmtOffset - currentGMTOffset;

    return [NSDate dateWithTimeInterval:gmtInterval sinceDate:sourceDate];
}

Yes, I know this next one is strange but my server gives me a whack date format

+(NSDate *)getDateFromString:(NSString *)dtStr
{
 NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
 NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
 [inputFormatter setLocale:locale];
 [locale release];
 [inputFormatter setDateFormat:@"MMMM, dd yyyy HH:mm:ss"];
 NSDate *formatterDate = [[inputFormatter dateFromString:dtStr] copy];
 [inputFormatter release];
 return formatterDate;
}


The first one doesn't, but the second one does, because you created a copy and didn't autorelease it. If you don't release it later, it will be leaked.

I don't see why you're even copying the date in the second method. Just cut that out and that will fix the leak.

You really should read (or re-read) the Memory Management Programming Guide for Cocoa, as it seems you need to refine your understanding of the memory-management rules.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜