如何释放这些泄漏?
我在我的应用程序中使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的内存泄漏发生在
+ (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.这适用于该控制器的所有方法,
只需在返回索引之前释放当前日历即可。
This works for me in all methods of this controller
Only need to release the current calendar before return the index.