Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8376751
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T15:29:57+00:00 2026-06-09T15:29:57+00:00

I created a clock for my app that I’m working on that starts off

  • 0

I created a clock for my app that I’m working on that starts off normal (counting by one second each time), but then freezes every two seconds, then every four, then eight, etc. until it reaches twenty and the app crashes, with no error in xcode showing, not even a Recieved Memory Warning.

I went to Instruments and found that when using my clock, my app’s memory was increasing at an alarming rate (although, any rate is alarming to me). The problem is, I can’t figure out what it is exactly that is causing the memory to go so high. The app is running on ARC, so I know that I didn’t forget to release something. The only thing I can think of is that each time the number is loaded it’s stored into memory and never released. How that is, I have no idea, and how to fix it I have even less of an idea. Please let me know what I can do. Here is my code:

-(void)updateTime {

    calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
    updateTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateTime) userInfo:nil repeats:YES];

    currentTime = [NSDate date];
    dateComponents = [calendar components:unitFlags fromDate:currentTime];
    year = [dateComponents year];
    month = [dateComponents month];
    day = [dateComponents day];
    hour = [dateComponents hour];
    minute = [dateComponents minute];
    second = [dateComponents second];

    if (hour >= 12) {
        lblAMPM.text = @"PM";
    } else {
        lblAMPM.text = @"AM";
    }

    //if (isTwelveHour == YES) {
        //make a 12 hrs clock instead of 24
        if (hour >= 12) {
            hour = 12 - (24-hour);
        }
    //}

    //if midnight, show hour as 12 instead of 0
    if (hour == 0) {
        hour = 12;
    }

    if (second < 10) {
        seconds = [NSString stringWithFormat:@"0%i", second];
    } else {
        seconds = [NSString stringWithFormat:@"%i", second];
    }

    if (minute < 10) {
        lblTime.text = [NSString stringWithFormat:@"%i:0%i:%@", hour, minute, seconds];
    } else {
        lblTime.text = [NSString stringWithFormat:@"%i:%i:%@", hour, minute, seconds];
    }

    [lblDate setText:[NSString stringWithFormat:@"%d/%d/%d", month, day, year]];

}

- (void) updateDateFirst:(int)firstComponent setSecond:(int)secondComponent{

    calendar = [NSCalendar currentCalendar];
    unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
    updateTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateTime) userInfo:nil repeats:YES];

    currentTime = [NSDate date];
    dateComponents = [calendar components:unitFlags fromDate:currentTime];
    year = [dateComponents year];
    month = [dateComponents month];
    day = [dateComponents day];

    [lblDate setText:[NSString stringWithFormat:@"%d/%d/%d", firstComponent, secondComponent, year]];

}

- (void)viewDidUnload {
    lblTime = nil;
    lblAMPM = nil;
    lblDate = nil;
    [super viewDidUnload];
}

I am calling updateTimer in viewDidAppear, just as an FYI.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T15:29:59+00:00Added an answer on June 9, 2026 at 3:29 pm

    Every time updateTime is called it creates a new timer and adds it to the current run loop when you call:

    updateTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateTime) userInfo:nil repeats:YES];
    

    The run loop retains these timers. So although you keep replacing the NSTimer pointed to by your ivar updateTimer they are never dealloc‘ed. ARC manages each NSTimer assigned to updateTimer so they have matching retain/release calls on them. However, the retain by the run loop when they are first added is never paired with a matching release since the timers are repeating and never invalidated. So the sequence goes like this:

    1. You call - (void) updateDateFirst:(int)firstComponent setSecond:(int)secondComponent to start things off and it creates an NSTimer.
    2. This timer fires 1 second later and calls updateTime, which creates a new timer. The original timer is repeating so it will fire again in 1 second. Now you have 2 timers.
    3. Both timers fire roughly 1 second later, call updateTime, and create 2 new timers. Now you have 4 timers….

    The standard way you would do this is just to create 1 repeating timer at the beginning and not in each call to updateTimer. Then when you are done, you call:

    [updateTimer invalidate];
    upateTimer = nil;
    

    This stops and frees the timer.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

hi i am doing one app here when i click button that time i
I am working on a Phonegap App that has dynamically created elements. The app
I have a click-once deployed app that uses a notification icon created using the
So I created this clock which displays Date and Time. Is there a more
I have an app that is a single QGlwidget with a right-click QMenu created
I'm diving into iOS development and have been working on an alarm clock app
I created a app that downloads all document libraries in a SP Site ,
So, I've created my wonderful winforms app that I want to unleash upon the
I created a MDI Delphi app in Delphi XE2 that connects to a DataSnap
I've created a small iphone app that contains a tab bar controller that has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.