Iphone 为什么这些方法会泄漏NSDate对象?
第一个:Iphone 为什么这些方法会泄漏NSDate对象?,iphone,objective-c,cocoa,Iphone,Objective C,Cocoa,第一个: + (NSDate*)convertToUTC:(NSDate*)sourceDate { NSTimeZone* currentTimeZone = [NSTimeZone localTimeZone]; NSTimeZone* utcTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"]; NSInteger currentGMTOffset = [currentTimeZone secondsFro
+ (NSDate*)convertToUTC:(NSDate*)sourceDate
{
NSTimeZone* currentTimeZone = [NSTimeZone localTimeZone];
NSTimeZone* utcTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"];
NSInteger currentGMTOffset = [currentTimeZone secondsFromGMTForDate:sourceDate];
NSInteger gmtOffset = [utcTimeZone secondsFromGMTForDate:sourceDate];
NSTimeInterval gmtInterval = gmtOffset - currentGMTOffset;
return [NSDate dateWithTimeInterval:gmtInterval sinceDate:sourceDate];
}
是的,我知道这下一个是奇怪的,但我的服务器给了我一个重击日期格式
+(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;
}
第一个没有,但是第二个有,因为您创建了一个副本并且没有自动释放它。如果你以后不发布它,它将被泄露 我不明白你为什么要用第二种方法复制日期。只要把它剪掉,就可以修补漏洞了
您确实应该阅读(或重读),因为您似乎需要完善对内存管理规则的理解。第一个没有,但第二个有,因为您创建了一个副本,但没有自动释放它。如果你以后不发布它,它将被泄露 我不明白你为什么要用第二种方法复制日期。只要把它剪掉,就可以修补漏洞了 您确实应该阅读(或重读),因为您似乎需要完善对内存管理规则的理解