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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:47:33+00:00 2026-05-25T21:47:33+00:00

Whenever I want to make a timer, I just do: [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(someMethod)

  • 0

Whenever I want to make a timer, I just do:

[NSTimer scheduledTimerWithTimeInterval:5.0 
                                 target:self 
                               selector:@selector(someMethod) 
                               userInfo:nil 
                                repeats:NO];

rather than

NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:5.0 
                                                  target:self 
                                                selector:@selector(someMethod) 
                                                userInfo:nil 
                                                 repeats:NO];

Is the first one a memory leak? What’s the correct way to use them?

  • 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-25T21:47:34+00:00Added an answer on May 25, 2026 at 9:47 pm

    You have no memory problem in either snippet; you simply have to make a choice about your needs for interacting with the timer.

    When you schedule a timer, the run loop retains it, and a reference to the timer is passed along when it fires and its selector — e.g., (void) doTimerThing: (NSTimer *)tim1 — is used, so it isn’t strictly necessary for you to put it into a variable and retain it yourself. Apple explains this pretty clearly in the “Memory Management” section of the Timer Programming Topics doc.

    If, however, you might want to invalidate the timer before it fires (or in between fires for a repeating timer), you do need to have a reference to it. In that case, it is also a good idea to call retain on it. The scheduledTimerWithTimeInterval:target:... method returns an object that you do not own, and you should not make assumptions about the memory status of objects you don’t own (including whether they are in an autorelease pool or not, or how long the run loop is going to keep the timer around). If you need the timer (or any object) to stick around, you should make a claim of ownership on it by calling retain.


    1 Note that a timer’s method should always have one parameter; the selector in your snippet looks like it is for a method that does not. The timer seems to work that way, but you’re operating contrary to the documented interface.

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

Sidebar

Related Questions

Whenever I want to modify a winform from another thread, I need to use
i'm using the DOMdocument class in php whenever i want to create a XML
When debugging, a trick I do whenever I want to exit the current method
I want my JTextPane to insert spaces whenever I press Tab. Currently it inserts
I want my web page to beep whenever a user exceeds the maximum character
I want code to run whenever I create a new object. For example, see
I want to send a message whenever RabbitMQ creates or deletes a queue, exchange
I want to get Default Windows Forms Unhandled-Exception Dialog whenever my C# application encounters
So I want to trigger an event (pausing/unpausing some media) whenever the user presses
Basically whenever someones opens up my (Google) map I want it default to their

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.