0

Hi I am basically making a simple timer amongst other things using NSTimer.

I have code to remember the start date (NSDate) and then a repeating timer fires every second to update the interface. I find that the timer fires correctly and the selector is called, but I get an exception on the first line. Any help is much appreciated.

The code with irrelevant bits missed:

#import <Foundation/Foundation.h>

@interface PPTTimer : NSObject
{
    NSTimer *mainTimer;
    NSDate *startTime;
    NSTimeInterval timerDuration;
    NSTimer *clockTimer;
    NSTimer *safariTimer;
}

- (void)startTimerForHours:(int)hours minutes:(int)mins;
- (void)pauseTimer;
- (void)resumeTimer;
- (void)finishTimer;
- (void)endTimer:(NSTimer *)theTimer;
- (void)updateClockFire:(NSTimer *)theTimer;
- (void)safariCheckFire:(NSTimer *)theTimer;

@property (assign) IBOutlet NSTextField *leftField;
@property (assign) IBOutlet NSTextField *rightField;
@property (assign) IBOutlet NSTextField *leftLabel;
@property (assign) IBOutlet NSTextField *rightLabel;

@end



#import "PPTTimer.h"

@implementation PPTTimer

@synthesize leftField = leftField_;
@synthesize rightField = rightField_;


- (void)startTimerForHours:(int)hours minutes:(int)mins
{
    startTime = [NSDate date];
    timerDuration = 60*(mins+60*hours);
    mainTimer = [NSTimer scheduledTimerWithTimeInterval:timerDuration target:self selector:@selector(endTimer:) userInfo:nil repeats:NO];
    clockTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(updateClockFire:) userInfo:nil repeats:YES];
    safariTimer = [NSTimer scheduledTimerWithTimeInterval:10 target:self selector:@selector(safariCheckFire:) userInfo:nil repeats:YES];
}

the selector code, screenshot with error:

https://i.stack.imgur.com/zkAo4.png

Here's (the relevant bit of) the AS code, remember that the PPTTimer has a blue cube and that is linked to the missing value…

property parent : class "NSObject"
    --app classes
    property PPTTimer : missing value
    --windows
    property winMain : missing value
    property winTimer : missing value
    --IB objects
    property hourField : missing value
    property minuteField : missing value

    on startTimerButton_(sender)
        winMain's orderOut_(sender)
        winTimer's makeKeyAndOrderFront_(sender)
        PPTTimer's startTimerForHours_minutes_(hourField's intValue(), minuteField's intValue())
    end startTimerButton_
Richard Birkett
  • 771
  • 9
  • 20
  • Only thing I can assume is that this is not garbage collected/ARC and you need to retain your variables, as well as release them in a dealloc method. [Retaining timers](http://stackoverflow.com/questions/4914906/question-about-nstimer-and-retain) is not necessary. – Joe Jun 06 '13 at 19:41
  • Or... you accidentally assigned startTime to some integral value elsewhere. – Joe Jun 06 '13 at 19:51
  • It's probably the retention (or lack thereof) the `PPTTimer` object itself. Can you show us how you've instantiated it? – Rob Jun 06 '13 at 19:59
  • The problem Rob is that this is an Obj-C class created by an ASOC app. Basically the way this is created is I have a blue cube object in the xib named as this class, it is linked to a missing value property in the script. This has worked fine before. And when the error occurs, see the that the timerDuration is 5400, doesn't that confirm the instance is still healthy? – Richard Birkett Jun 06 '13 at 20:15
  • I'd be inclined to put a `NSLog` in the `dealloc` of `PTTimer` just to make sure. Also, are you doing an `invalidate` of your repeating timers at the appropriate point? – Rob Jun 06 '13 at 21:40
  • Well, the thing is that when i retain startTime, it's fine, but why is this necessary!? It's an ivar :/ … and I am invalidating correctly :) – Richard Birkett Jun 06 '13 at 22:03

0 Answers0