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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T19:55:24+00:00 2026-05-13T19:55:24+00:00

I’m having problems starting & stopping NSTimers. The docs say that a timer is

  • 0

I’m having problems starting & stopping NSTimers. The docs say that a timer is stopped by [timer invalidate];

I have a timer object declared as such

.h
NSTimer *incrementTimer;
@property (nonatomic, retain) NSTimer *incrementTimer;
.m
@synthesize incrementTimer;
-(void)dealloc {
 [incrementTimer release];
 [super dealloc];
 }

-The usual.

When it’s needed, my method does the following:

-(void)setGenCount {
    if(!condition1 && condition2) {
        incrementTimer = [NSTimer scheduledTimerWithTimeInterval: 2.0 
                                                      target: self 
                                                    selector:@selector(incrementBatteryVoltage:) 
                                                    userInfo: nil 
                                                     repeats: YES]; 
    }
}

Everything above works fine. However, once that timer does it’s job, I want it to invalidate itself. I invalidate the timer because there is an equal decrement method that could be called and would fight against the incrementTimer if it was still active. (Previously, I noticed that my two timers, if active, were acting on the same ivar by increasing & decreasing the value (a sort of fight)… without crashing) The selector called works as follows:

-(void)incrementBatteryVoltage:(NSTimer *)timer {
    if(battVoltage < 24.0) {
         generatorDisplay.battVoltage += 0.1;
      }
    if(battery1Voltage == 24.0) {
         [timer invalidate];
      }
  }

I have an equal method that Decrements the battery count. (previously mentioned)
Due to my program design: the interface simulates a voltage display. When the “machine” is turned off, I want all the timers invalidated, regardless of what any voltage value is. I’m doing this by checking to see if the timer is valid.

-(void)deEnergizeDisplays {

   if([decrementTimer isValid]) {
        [decrementTimer invalidate];
        decrementTimer = nil;
     }

    if([incrementTimer isValid]) {
       [incrementTimer invalidate];
       incrementTimer = nil;
    }

I’m getting numerous “BAD_ACCESS” crashes. The erroneous line call is always pointing toward my [timer isValid] call. It seems that if the timer is invalidated… the pointer
doesn’t exist either. I know that the [timer invalidate] message disables the timer and then it is removed from the run loop and then it is released. And my understanding is: it is an autoreleased object per it’s naming covention.

My thought are: If I’m sending a retain message, shouldn’t the reference still exist? I’ve tried several combinations, taking away:

timer = nil;

or even instead of:

if([timer isValid])

I tried :

if([timer != nil])

and:

if(timer)

I always get the same crash. Thanks for any help on starting & stopping NSTimers.

  • 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-13T19:55:24+00:00Added an answer on May 13, 2026 at 7:55 pm

    UPDATE: See Darren’s answer. The problem is that you are not using your property accessor when setting the timers. Instead of:

    incrementTimer = [NSTimer ...
    

    You should have:

    self.incrementTimer = [NSTimer ...
    

    The self.propertyName = ... syntax will call your accessor method, and thereby automatically retain the object that you send to it (since your property is set up as retain). Simply calling propertyName = ... does not use the property accessor. You are simply changing the value of your ivar directly.


    UPDATE #2: After an enlightening conversation with Peter Hosey (see comments), I have removed my earlier suggestion to “never retain or release” your timer object. I have also completely re-written my earlier code because I think the following is a better approach:

    Controller.h:

    NSTimer *voltageTimer;
    float targetBatteryVoltage;
    ...
    @property (nonatomic, retain) NSTimer *voltageTimer;
    

    Controller.m:

    @implementation Controller
    @synthesize voltageTimer;
    
    - (void)stopVoltageTimer {
        [voltageTimer invalidate];
        self.voltageTimer = nil;
    }
    
    - (void)setTargetBatteryVoltage:(float)target {
        [voltageTimer invalidate];
        targetBatteryVoltage = target;
        self.voltageTimer = [NSTimer scheduledTimerWithTimeInterval: 2.0
                                    target: self
                                  selector: @selector(updateBatteryVoltage:)
                                  userInfo: nil
                                   repeats: YES];
    }
    
    - (void)updateBatteryVoltage:(NSTimer *)timer {
        const float increment = 0.1;
        if (abs(battVoltage - targetBatteryVoltage) < increment) {
            [timer invalidate];
        }
        else if (battVoltage < targetBatteryVoltage) {
            generatorDisplay.battVoltage += increment;
        }
        else if (battVoltage > targetBatteryVoltage) {
            generatorDisplay.battVoltage -= increment;
        }
    }
    

    Now, you can simply set a target battery voltage, and the timer magic will happen behind the scenes:

    [self setTargetBatteryVoltage:24.0];
    

    Your power-off method would look as follows:

    - (void)deEnergizeDisplays {
        [self stopVoltageTimer];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 382k
  • Answers 382k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The Clear method on a listview does exactly that. Just… May 14, 2026 at 10:35 pm
  • Editorial Team
    Editorial Team added an answer Use zero-width matching assertions: String str = "la$le\\$li$lo"; System.out.println(java.util.Arrays.toString( str.split("(?<!\\\\)\\$")… May 14, 2026 at 10:35 pm
  • Editorial Team
    Editorial Team added an answer Fully qualify the exception type. public void insertIntoCell.. throws com.sun.star.lang.IndexOutOfBoundsException… May 14, 2026 at 10:35 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.