2011-05-02 4 views
1

간단한 타이머를 실행하는 앱이 있습니다. 다음 코드는 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]; 

} 

어떤 도움을 많이 주시면 감사하겠습니다!

답변

2

dealloc이 아닌 이 (가) 호출됩니다. calendar에는 더 이상 유지할 항목이 없을 때 프레임 워크가 dealloc을 호출합니다.

+0

영웅입니다. 고마워요. - 완벽하게 작동합니다. 그리고 나는이 모든 시간을 잘못 지워 버렸습니다. 감사. – Rich

관련 문제