0

Now I know there are questions on SO about NSTimer pause, but none on repeatable and closing/opening windows. Toggeling pause works perfect (checked using button). Reopening window gives BAD_ACCESS error.

- (void)awakeFromNib
{
    self.timer = [[NSTimer scheduledTimerWithTimeInterval:0.85
                                               target:self
                                             selector:@selector(populateTable:)
                                             userInfo:nil
                                              repeats:YES] retain];
}

-(IBAction)fireUpTableWindow:(id)sender
{
    [aTableWindow makeKeyAndOrderFront:self];
    [self resumeTimer:timer];
}


NSDate *pauseStart, *previousFireDate;

-(void) pauseTimer:timer
{
    pauseStart = [[NSDate dateWithTimeIntervalSinceNow:0] retain];    
    previousFireDate = [[timer fireDate] retain];
    [timer setFireDate:[NSDate distantFuture]];
}

-(void) resumeTimer:timer
{
    float pauseTime = -1*[pauseStart timeIntervalSinceNow];
    [timer setFireDate:[previousFireDate initWithTimeInterval:pauseTime sinceDate:previousFireDate]];
    [pauseStart release];
    [previousFireDate release];
}

- (IBAction)closeTableWindow:(id)sender
{
    [self pauseTimer:timer];
    [self->aTableWindow close];
    [self release];
}
}
Jeremiah Smith
  • 740
  • 6
  • 17
  • You shouldn't put retain on your timer..thats only going to cause problems – Tommy Devoy May 22 '13 at 17:26
  • @ttarules still got the same error without retaining the timer... and I did try [timer invalidate] at closeTableWindow as well, didn't work sadly... – Jeremiah Smith May 22 '13 at 17:33
  • @ttarules and I also tried to with deleting all retaining (also in resumeTimer etc.) – Jeremiah Smith May 22 '13 at 17:46
  • Also it uses: - (void) dealloc { [timer invalidate]; [timer release]; timer = nil; [super dealloc]; } And I was thinking of creating entire new timer after invalidating it at closeTableWindow... – Jeremiah Smith May 22 '13 at 18:32
  • In header file: @property (assign) NSTimer *timer; – Jeremiah Smith May 22 '13 at 19:09
  • There's nothing wrong with retaining a timer, @ttarules, provided that you release it appropriately. – jscs May 22 '13 at 19:26
  • _Reopening window gives BAD_ACCESS error._ Check in Interface Builder that the Window is not set to _Release on close_. – Gerd K May 22 '13 at 20:16
  • @GerdK this is it! That makes it work, strange lol. Can you make this an answer, I could then mark it as answered, thanks so much. – Jeremiah Smith May 22 '13 at 21:15
  • @Jeremiah Smith: Thanks, done. Glad it worked out, I have fallen for this one before hence it rang a bell. – Gerd K May 23 '13 at 03:10

1 Answers1

0

Reopening window gives BAD_ACCESS error.

Check in Interface Builder that the Window is not set to Release on Close.

Gerd K
  • 2,933
  • 1
  • 20
  • 23