不能找到内存泄漏的地方在这段代码
问题描述:
我有一个应用程序正在运行一个简单的计时器。以下代码每秒从NSTimer运行几次。我将首先承认,作为一名新的iOS开发人员,内存管理是我目前最弱的技能。当我运行这段代码时,如果我让计时器运行一段时间,我开始得到内存警告,并最终崩溃。如果我禁用NSTimer,它可以运行很长时间。我不知道是什么导致泄漏:不能找到内存泄漏的地方在这段代码
- (void)onTimerTick
{
NSDate *date = [NSDate date];
NSCalendar *calendar= [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSCalendarUnit unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
NSDateComponents *dateComponents = [calendar components:unitFlags fromDate:date];
NSInteger hour = [dateComponents hour];
NSInteger min = [dateComponents minute];
NSInteger sec = [dateComponents second];
double milliSince1970 = [date timeIntervalSince1970];
int secsSince1970 = [date timeIntervalSince1970];
int frame = (((milliSince1970 - secsSince1970) * 1000)/frameDuration) + 1;
timeCode.text = [NSString stringWithFormat:@"%d:%d:%d:%d", hour, min, sec, frame];
[calendar dealloc];
}
任何帮助将不胜感激!
答
呼叫[calendar release]
,不dealloc
...框架调用dealloc
你,当calendar
不再有任何保留它。
哦,你是英雄。非常感谢 - 它的工作完美,我知道我一直在删除错误的对象!谢谢。 – Rich 2011-05-02 22:24:32