2011-04-01 65 views
0

我有一個小方法返回一個給定日期的第一個星期天:內存泄漏日期的東西(__NSCFCalendar,ICU ::的GregorianCalendar)

- (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」變量,由於返回而必須自動釋放的變量。

這些是由性能工具中發現的泄漏對象:

  • ICU :: GregorianCalendar的(1.00 KB)
  • ICU ::的SimpleTimeZone(112個字節)
  • __NSDate(16個字節)
  • ICU :: NSNumberingSystem(128個字節)
  • __NSCFCalendar(48個字節)

錯誤必須是完全愚蠢的,但我找不到它。你可以幫我嗎?謝謝!!

回答

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, init,或copy

+0

是的,icu部分看起來也很腥。謝謝你的快速回答,希望別人現在可能會對這些icu對象有所瞭解。 – 2011-04-01 19:18:55

+0

也可以查看任何調用此方法的代碼。它很可能保留價值而不是釋放,我的猜測是將其設置爲某個未正確發佈的屬性。正如amattn所說,你的泄漏在該代碼中不存在。 – Joe 2011-04-01 19:29:00