invalidate NSTimer instance in destroy to prevent memory leak

This commit is contained in:
minggo 2014-05-28 15:19:07 +08:00
parent 013b3a328f
commit 2e70380dd9
2 changed files with 19 additions and 13 deletions

View File

@ -34,6 +34,7 @@
}
@property (readwrite) int interval;
-(void) startMainLoop;
-(void) stopMainLoop;
-(void) doCaller: (id) sender;
-(void) setAnimationInterval:(double)interval;
+(id) sharedDirectorCaller;

View File

@ -58,6 +58,7 @@ static id s_sharedDirectorCaller;
+(void) destroy
{
[s_sharedDirectorCaller stopMainLoop];
[s_sharedDirectorCaller release];
s_sharedDirectorCaller = nil;
}
@ -76,25 +77,29 @@ static id s_sharedDirectorCaller;
-(void) startMainLoop
{
// Director::setAnimationInterval() is called, we should invalidate it first
[displayLink invalidate];
displayLink = nil;
displayLink = [NSClassFromString(@"CADisplayLink") displayLinkWithTarget:self selector:@selector(doCaller:)];
[displayLink setFrameInterval: self.interval];
[displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[self stopMainLoop];
displayLink = [NSClassFromString(@"CADisplayLink") displayLinkWithTarget:self selector:@selector(doCaller:)];
[displayLink setFrameInterval: self.interval];
[displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
}
-(void) stopMainLoop
{
[displayLink invalidate];
displayLink = nil;
}
-(void) setAnimationInterval:(double)intervalNew
{
// Director::setAnimationInterval() is called, we should invalidate it first
[displayLink invalidate];
displayLink = nil;
// Director::setAnimationInterval() is called, we should invalidate it first
[self stopMainLoop];
self.interval = 60.0 * intervalNew;
self.interval = 60.0 * intervalNew;
displayLink = [NSClassFromString(@"CADisplayLink") displayLinkWithTarget:self selector:@selector(doCaller:)];
[displayLink setFrameInterval: self.interval];
[displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
displayLink = [NSClassFromString(@"CADisplayLink") displayLinkWithTarget:self selector:@selector(doCaller:)];
[displayLink setFrameInterval: self.interval];
[displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
}
-(void) doCaller: (id) sender