Note that there are some explanatory texts on larger screens.

plurals
  1. POProblems invalidating & re-creating NSTimer(s)
    primarykey
    data
    text
    <p>I'm having problems starting &amp; stopping NSTimers. The docs say that a timer is stopped by [timer invalidate];</p> <p>I have a timer object declared as such</p> <pre><code>.h NSTimer *incrementTimer; @property (nonatomic, retain) NSTimer *incrementTimer; .m @synthesize incrementTimer; -(void)dealloc { [incrementTimer release]; [super dealloc]; } </code></pre> <p>-The usual.</p> <p>When it's needed, my method does the following:</p> <pre><code>-(void)setGenCount { if(!condition1 &amp;&amp; condition2) { incrementTimer = [NSTimer scheduledTimerWithTimeInterval: 2.0 target: self selector:@selector(incrementBatteryVoltage:) userInfo: nil repeats: YES]; } } </code></pre> <p>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 &amp; decreasing the value (a sort of fight)... without crashing) The selector called works as follows: </p> <pre><code>-(void)incrementBatteryVoltage:(NSTimer *)timer { if(battVoltage &lt; 24.0) { generatorDisplay.battVoltage += 0.1; } if(battery1Voltage == 24.0) { [timer invalidate]; } } </code></pre> <p>I have an equal method that Decrements the battery count. (previously mentioned)<br> 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.</p> <pre><code>-(void)deEnergizeDisplays { if([decrementTimer isValid]) { [decrementTimer invalidate]; decrementTimer = nil; } if([incrementTimer isValid]) { [incrementTimer invalidate]; incrementTimer = nil; } </code></pre> <p>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.</p> <p>My thought are: If I'm sending a retain message, shouldn't the reference still exist? I've tried several combinations, taking away:</p> <pre><code>timer = nil; </code></pre> <p>or even instead of: </p> <pre><code>if([timer isValid]) </code></pre> <p>I tried :</p> <pre><code>if([timer != nil]) </code></pre> <p>and: </p> <pre><code>if(timer) </code></pre> <p>I always get the same crash. Thanks for any help on starting &amp; stopping NSTimers.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload