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 6154851
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T20:19:50+00:00 2026-05-23T20:19:50+00:00

I have an NSTimer firing at 60 fps. It updates a C++ model and

  • 0

I have an NSTimer firing at 60 fps. It updates a C++ model and then draws via Quartz 2D. This works well except memory accumulates quickly even though I am not allocating anything. Instruments reports no leaks but many CFRunLoopTimers (I guess from the repeating NSTimer?) seem to be accumulating. Clicking the window or pressing a key purges most of them which would seem to point to an autorelease pool not being drained frequently enough. Do I have to rely on events to cycle the autorelease pool(s) or is there a better way to clear out the memory?

Any help is appreciated, Thanks

-Sam

Timer creation (timer is an ivar):

timer = [NSTimer scheduledTimerWithTimeInterval:1.0f / 60 target:self selector:@selector(update:) userInfo:nil repeats:YES];

update: method:

- (void)update:(NSTimer *)timer {
    controller->Update();
    [self.view setNeedsDisplay:YES];
}

Update:

After messing around with this a little more I’ve made a couple of additional observations.

1.) [self.view setNeedsDisplay:YES] seems to be the culprit in spawning these CFRunLoopTimers. Replacing it with [self.view display] gets rid of the issue but at the cost of performance.

2.) Lowering the frequency to 20-30 fps and keeping `[self.view setNeedsDisplay:YES]’ also causes the issue to go away.

This would seem to imply setNeedsDisplay: doesn’t like to be called a lot (maybe more time’s per second then can be displayed?). I frankly can’t understand what the problem with “overcalling” it if all it does is tell the view to be redisplayed at the end of the eventloop.

I am sure I am missing something here and any additional help is greatly appreciated.

  • 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-05-23T20:19:52+00:00Added an answer on May 23, 2026 at 8:19 pm

    Usually the right solution would be to create a nested NSAutoreleasePool around your object-creations-heavy code.

    But in this case, it seems the objects are autoreleased when the timer re-schedule itself — a piece of code you can’t control. And you can’t ask the topmost autorelease pool to drain itself without releasing it.

    In your case, the solution would be to drop your NSTimer for frame-rate syncing, and to use a CADisplayLink instead:

    CADisplayLink *frameLink = [CADisplayLink displayLinkWithTarget:self
                                                            selector:@selector(update:)];
    
    // Notify the application at the refresh rate of the display (60 Hz)
    frameLink.frameInterval = 1;
    
    [frameLink addToRunLoop:[NSRunLoop mainRunLoop]
                    forMode:NSDefaultRunLoopMode];
    

    CADisplayLink is made to synchronize the drawing to the refresh rate of the screen — so it seems like a good candidate for what you want to do. Besides, NSTimer are not precise enough to sync with the display refresh rate when running at 60 Hz.

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

Sidebar

Related Questions

I have created a NSTimer In Xcode 4.2 and it works but i get
i have this timer [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateTimerFunc) userInfo:nil repeats:YES]; and on some function
I have an NSTimer that I init with this code: testTimer = [[NSTimer alloc]
Possible Duplicate: NSTimer doesn't stop I have this code: [NSTimer scheduledTimerWithTimeInterval:110.0 target:self selector:@selector(targetMethod:) userInfo:nil
I have an NSTimer which is setup like this: [NSTimer scheduledTimerWithTimeInterval:0.5 target:timerTarget selector:NSSelectorFromString(targetSelector) userInfo:nil
I have a NSTimer object that I need to invalidate if a user taps
I have an NSTimer running in my application that collects some data and sends
I have created an app that uses NSTimer, which gets triggered each second. My
I need to start NSTimer by 0:0:0 that I am doing and I have
I have created an animated game app, In which I am using NSTimer to

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.