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; @property (readwrite) int interval;
-(void) startMainLoop; -(void) startMainLoop;
-(void) stopMainLoop;
-(void) doCaller: (id) sender; -(void) doCaller: (id) sender;
-(void) setAnimationInterval:(double)interval; -(void) setAnimationInterval:(double)interval;
+(id) sharedDirectorCaller; +(id) sharedDirectorCaller;

View File

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