thread exist safety fixup

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/performance/trunk@36058 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
Richard Frith-MacDonald 2013-02-03 06:37:42 +00:00
parent bd30ad1517
commit dd0b17ac3c
2 changed files with 26 additions and 15 deletions

View file

@ -1,3 +1,9 @@
2013-02-03 Richard Frith-Macdonald <rfm@gnu.org>
* GSTicker.m: Retain timer so that we can safely invalidate it even
on thread exit when the runloop of the current thread may have been
deallocated already.
2012-01-11 Niels Grewe <niels.grewe@halbordnung.de> 2012-01-11 Niels Grewe <niels.grewe@halbordnung.de>
* GSSkipMutableArray.m: Change -initWithObjects:count: declaration * GSSkipMutableArray.m: Change -initWithObjects:count: declaration

View file

@ -66,6 +66,12 @@ static volatile NSTimeInterval lastTime = 0;
@interface GSTickerThread : NSObject @interface GSTickerThread : NSObject
{ {
@public @public
/* NB. We retain theTimer rather than depending on the run loop to do it.
* This is because tis object is typically deallocated on thread exist,
* so the thread's run loop may be deallocated before this object is
* deallocated, and we want to be sure the timer is not already deallocated
* when we invalidate it.
*/
NSTimer *theTimer; NSTimer *theTimer;
NSMutableArray *observers; NSMutableArray *observers;
unsigned last; unsigned last;
@ -76,6 +82,7 @@ static volatile NSTimeInterval lastTime = 0;
- (void) dealloc - (void) dealloc
{ {
[theTimer invalidate]; [theTimer invalidate];
[theTimer release];
theTimer = nil; theTimer = nil;
[observers release]; [observers release];
observers = nil; observers = nil;
@ -88,11 +95,11 @@ static volatile NSTimeInterval lastTime = 0;
NSTimeInterval ti = GSTickerTimeNow(); NSTimeInterval ti = GSTickerTimeNow();
observers = [NSMutableArray new]; observers = [NSMutableArray new];
theTimer = [NSTimer scheduledTimerWithTimeInterval: ti - (int)ti theTimer = [[NSTimer scheduledTimerWithTimeInterval: ti - (int)ti
target: [GSTicker class] target: [GSTicker class]
selector: @selector(_tick:) selector: @selector(_tick:)
userInfo: self userInfo: self
repeats: NO]; repeats: NO] retain];
} }
return self; return self;
} }
@ -307,11 +314,9 @@ NSTimeInterval GSTickerTimeNow()
{ {
NSTimeInterval ti; NSTimeInterval ti;
if (tt->theTimer != t) [tt->theTimer invalidate];
{ [tt->theTimer release];
[tt->theTimer invalidate]; tt->theTimer = nil;
tt->theTimer = nil;
}
if ([tt->observers count] > 0) if ([tt->observers count] > 0)
{ {
@ -339,11 +344,11 @@ NSTimeInterval GSTickerTimeNow()
} }
ti = GSTickerTimeNow(); ti = GSTickerTimeNow();
tt->theTimer = [NSTimer scheduledTimerWithTimeInterval: ti - (int)ti tt->theTimer = [[NSTimer scheduledTimerWithTimeInterval: ti - (int)ti
target: self target: self
selector: @selector(_tick:) selector: @selector(_tick:)
userInfo: tt userInfo: tt
repeats: NO]; repeats: NO] retain];
} }
else else
{ {