内存泄漏与日期的东西(__ NSCFCalendar,icu :: GregorianCalendar)

时间:2011-04-01 19:02:49

标签: iphone ios memory-leaks gregorian-calendar

我有一个小方法返回给定日期的第一个星期 -

- (NSDate*) getFirstDayOfTheWeekFor:(NSDate*)date {

NSCalendar *gregorianCalender = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
NSDate *firstDayDate; //This is 100.0% leaking acc. to the Performance Tool Leaks

unsigned yearAndWeek = NSYearCalendarUnit | NSWeekCalendarUnit;

// retrieve the components from the current date
NSDateComponents *compsCurrentDate = [[gregorianCalender components:yearAndWeek fromDate:date] autorelease];

[compsCurrentDate setWeekday:2]; // Monday
[compsCurrentDate setHour:0];
[compsCurrentDate setMinute:0];
[compsCurrentDate setSecond:0];

// make a date from the modfied components
firstDayDate = [[gregorianCalender dateFromComponents:compsCurrentDate] autorelease];   

return firstDayDate;
}

正如您所看到的,我已经尝试自动释放此处使用的每个变量(这不是我开始追踪泄漏之前的样子)。最初我想在返回之前显式释放所有变量,除了“firstDayDate”变量,由于返回,HAS将被自动释放。

这些是Performance Tool找到的泄漏对象:

  • icu :: GregorianCalendar(1.00 KB)
  • icu :: SimpleTimeZone(112字节)
  • __ NSDate(16字节)
  • icu :: NSNumberingSystem(128字节)
  • __ NSCFCalendar(48字节)

错误必须是完全愚蠢的东西,但我找不到它。你能帮助我吗?谢谢!!

1 个答案:

答案 0 :(得分:0)

它应该是这样的:

- (NSDate*) getFirstDayOfTheWeekFor:(NSDate*)date
{
    NSCalendar *gregorianCalender = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
    NSDate *firstDayDate; //This is 100.0% leaking acc. to the Performance Tool Leaks

    unsigned yearAndWeek = NSYearCalendarUnit | NSWeekCalendarUnit;

    // retrieve the components from the current date
    NSDateComponents *compsCurrentDate = [gregorianCalender components:yearAndWeek fromDate:date];

    [compsCurrentDate setWeekday:2]; // Monday
    [compsCurrentDate setHour:0];
    [compsCurrentDate setMinute:0];
    [compsCurrentDate setSecond:0];

    // make a date from the modfied components
    firstDayDate = [gregorianCalender dateFromComponents:compsCurrentDate];

    return firstDayDate;
}

如果firstDayDate泄漏,则不在此方法中。检查下游。此外,icu部分看起来有点可疑。它可能是围绕icu库的iOS包装器中的错误/泄漏。

如果您releaseautorelease,请仅记住alloc, initcopy