我正在开发一款应用程序,它基本上都运行良好。但显然还不够好。当运行“泄漏”仪器时,我看到NSTimer发现了很多泄漏。
使用UISwitch启动和停止NSTimer。每次开关切换到“OFF”时都会发生内存泄漏
//PlayView.h
@interface PlayView : UIViewController {
NSTimer *autoTimer;
// other things
}
@property (nonatomic, retain) NSTimer *autoTimer;
// etc...
//PlayView.m
#import "PlayView.h"
@implementation PlayView
@synthesize autoTimer;
- (IBAction) toggleEnabledForSwitch1: (id) sender {
if (switch1.on) {
self.autoTimer = [NSTimer scheduledTimerWithTimeInterval:0.5
target:self
selector:@selector(updateCounter:)
userInfo:nil
repeats:YES];
}
else {
restart = 1;
[self.autoTimer invalidate];
self.autoTimer = nil;
[self.autoTimer release];
}
}
- (void)updateCounter:(NSTimer *)theTimer {
// Do a bunch of stuff
}
可能导致这种情况的原因是什么?我确定它一定是简单的,我只是想念它......