日期内容的内存泄漏(__NSCFCalendar,icu::GregorianCalendar)

发布于 2024-10-29 08:45:20 字数 1189 浏览 1 评论 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] 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 字节)

错误一定是某些东西完全愚蠢,但我找不到它。你能帮助我吗?谢谢你!!

I am having a small method returning the first week-day of a given date:

- (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;
}

As you can see, I am already trying to autorelease every single variable that is used here (this is not what it looked like before I started to track down the leaks). Originally I wanted to explicitly release all variables before the return, except the "firstDayDate" variable, which HAS to be autoreleased because of the return.

These are the Leaked Objects found by the Performance Tool:

  • icu::GregorianCalendar (1.00 KB)
  • icu::SimpleTimeZone (112 Bytes)
  • __NSDate (16 Bytes)
  • icu::NSNumberingSystem (128 Bytes)
  • __NSCFCalendar (48 Bytes)

The mistake MUST be something completely stupid but I can't find it. Can you help me? THANK YOU!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

土豪我们做朋友吧 2024-11-05 08:45:21

它应该看起来像这样:

- (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 包装器中的错误/泄漏。

请记住,如果您alloc、initcopy,则只能releaseautorelease

It should look like this:

- (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;
}

If firstDayDate is leaking, it's not in this method. Check downstream. Also, the icu part looks a little fishy to me. It could be a bug/leak in iOS wrapper around the icu library.

Remember you only release or autorelease if you alloc, init, or copy.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文