如何释放这些泄漏?

发布于 2024-12-10 20:08:11 字数 852 浏览 0 评论 0原文

我在我的应用程序中使用 THCalendarInfo 类。在这个类中,我遇到了内存泄漏,但实际上泄漏显示在这一行 CFCalendarCopyCurrent(), 处。实际上在dealloc方法中它被释放了,但是它显示了内存泄漏。我发送示例代码。

+ (int) currentDayOfWeek
{
    return CFCalendarGetOrdinalityOfUnit (
        CFCalendarCopyCurrent(),
        kCFCalendarUnitDay,
        kCFCalendarUnitWeek,
    [self currentAbsoluteTime]
    );
}

- (id) init
{
    [super init];
    _absoluteTime = CFAbsoluteTimeGetCurrent();
    _calendar     = CFCalendarCopyCurrent();
    _timeZone     = CFCalendarCopyTimeZone( _calendar );
    _dayNames     = nil;
    _monthNames   = nil;

    [self setupEnglishNames];
    return self;
}

- (void) dealloc
{
    if ( _calendar ) CFRelease( _calendar );
    if ( _timeZone ) CFRelease( _timeZone );

    [_dayNames release];
    [_monthNames release];

    [super dealloc];
}

I am using THCalendarInfo class in my application. In this class i got memory leaks, but actually the leak shows at this line CFCalendarCopyCurrent(),. Actually in dealloc method it is released, but it shows the memory leaks. I send the sample code.

+ (int) currentDayOfWeek
{
    return CFCalendarGetOrdinalityOfUnit (
        CFCalendarCopyCurrent(),
        kCFCalendarUnitDay,
        kCFCalendarUnitWeek,
    [self currentAbsoluteTime]
    );
}

- (id) init
{
    [super init];
    _absoluteTime = CFAbsoluteTimeGetCurrent();
    _calendar     = CFCalendarCopyCurrent();
    _timeZone     = CFCalendarCopyTimeZone( _calendar );
    _dayNames     = nil;
    _monthNames   = nil;

    [self setupEnglishNames];
    return self;
}

- (void) dealloc
{
    if ( _calendar ) CFRelease( _calendar );
    if ( _timeZone ) CFRelease( _timeZone );

    [_dayNames release];
    [_monthNames release];

    [super dealloc];
}

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

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

发布评论

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

评论(2

GRAY°灰色天空 2024-12-17 20:08:11

您的内存泄漏发生在 + (int) currentDayOfWeek 方法中,因为您创建了逻辑日历的另一个副本。尝试使用现有日历而不是创建新日历。

Your memory leak is in the + (int) currentDayOfWeek method because you create an another copy of the logical calendar. Try to use existed calendar instead of creating a new one.

巷雨优美回忆 2024-12-17 20:08:11

这适用于该控制器的所有方法,

只需在返回索引之前释放当前日历即可。

+ (int) currentDayOfWeek
{
    CFCalendarRef currentCalendar = CFCalendarCopyCurrent ();

    CFIndex respuesta = CFCalendarGetOrdinalityOfUnit (
                                           currentCalendar,
                                           kCFCalendarUnitDay,
                                           kCFCalendarUnitWeek,
                                           [self currentAbsoluteTime]
                                           );
    CFRelease(currentCalendar);

    return respuesta;
}

+ (int) daysInCurrentMonth
{
    CFCalendarRef currentCalendar = CFCalendarCopyCurrent ();

    CFRange r = CFCalendarGetRangeOfUnit (
       currentCalendar,
       kCFCalendarUnitDay,
       kCFCalendarUnitMonth,
       [self currentAbsoluteTime]
    );


    CFRelease(currentCalendar);

    return r.length;
}

This works for me in all methods of this controller

Only need to release the current calendar before return the index.

+ (int) currentDayOfWeek
{
    CFCalendarRef currentCalendar = CFCalendarCopyCurrent ();

    CFIndex respuesta = CFCalendarGetOrdinalityOfUnit (
                                           currentCalendar,
                                           kCFCalendarUnitDay,
                                           kCFCalendarUnitWeek,
                                           [self currentAbsoluteTime]
                                           );
    CFRelease(currentCalendar);

    return respuesta;
}

+ (int) daysInCurrentMonth
{
    CFCalendarRef currentCalendar = CFCalendarCopyCurrent ();

    CFRange r = CFCalendarGetRangeOfUnit (
       currentCalendar,
       kCFCalendarUnitDay,
       kCFCalendarUnitMonth,
       [self currentAbsoluteTime]
    );


    CFRelease(currentCalendar);

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