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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:47:01+00:00 2026-05-31T13:47:01+00:00

I have an issue while working with NSTimer. Let’s assume I have this architecture

  • 0

I have an issue while working with NSTimer.
Let’s assume I have this architecture :

ThreadedClass.m (contains a NSTimer* timer;)

- (id) init {
  if (self = [super init]) {
    // do blablabla
    [self launchAThread];
  }
  return self;
}


- (void) launchAThread {
    [NSThread detachNewThreadSelector:@selector(selectorToMyThreadFunction)
                             toTarget:self
                           withObject:nil];
}


- (void) selectorToMyThreadFunction {
    //I do my stuff in here
    //Then i relaunch a Timer to call this function
    //periodically but it has to be "atomic" so no
    //repeating timer since i don't know the time
    //this function will take

    //I do some [self changeSomething];

    [self restartTimer];

    //MyThread ends here (and might be recreated by the Timer's bip
}

- (void)restartTimer {
    if (![NSThread isMainThread]) {
      [self performSelectorOnMainThread:@selector(restartTimer)
                             withObject:nil
                          waitUntilDone:NO];
      return;
    }

    [timer invalidate];
    [timer release];
    timer = [[NSTimer scheduledTimerWithTimeInterval:interval
                                              target:self
                                            selector:@selector(launchWithTimer:)
                                            userInfo:nil
                                             repeats:NO] retain];
}

- (void) launchWithTimer:(NSTimer *)theTimer {
  if (theTimer == timer)
  {
    [timer release];
    timer = nil;
    [self launchAThread];
  }
  else
  {
    //Nothing to be done in here, a user launch a thread manually
  }
}

So let’s assume the user of the class alloc it and release it right after. My timer will still be alive and the object too (since there is a retain made by the timer).
When the timer will fire, it will do [self launchAThread] and then the timer will invalidate and release itself AND it will release my object which now has a retainCount = 0… Let’s assume, one more time, the object is deallocted right after, this will cause crash and there is nothing i can do to stop it that comes right to my mind.

I agree, this is a lot of assumptions but i’m curious to know if someone already had this issue and how he solved it.

Thanks for reading and I hope I was clear ! 🙂

  • 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-31T13:47:03+00:00Added an answer on May 31, 2026 at 1:47 pm

    Since I’m making a library, i can’t assume that the user will call a cleanUp function anytime.

    So to solve my problem, I added a new “layer” : a new class that will do the thread and timer part so that I am the user of this class and I know I have to cleanUp !

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

Sidebar

Related Questions

I've been working this issue for a while now and I am open to
I have this Java-Castor issue while mapping a hierarchy. Thanks for your time. I
While working with ASP.NET using Visual Studio (2008) I have discomfort issue: source code
I have an odd issue while using FlashCS4. I have a textfield that, when
First of all: I'm at Scala 2.8 I have a slight issue while using
I have ran into an interesting issue while trying to create a more usable
I have an issue with log4net which has been bugging me for a while
While debugging an issue with our system, I have discovered a thread contention that
I have little issue with detecting internet connection while using static IP on device.
I'm running into an issue while unit-testing a Python project that I'm working on

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.