NSTimer 内存泄漏?
我正在开发一个应用程序,它基本上一切正常并且运行良好。但显然还不够好。运行“Leaks”工具时,我发现 NSTimer 存在大量泄漏。
NSTimer 通过 UISwitch 启动和停止。每次将开关切换到“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
}
这可能是什么原因造成的?我确信这一定是简单的事情,但我只是想念它......
I'm developing an app and its essentially all up and running well. Obviously not well enough, though. When running the "Leaks" instrument, I'm seeing a lot of leaks from my NSTimer.
The NSTimer is started and stopped with a UISwitch. The memory leak occurs each time the switch is flipped to "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
}
What could be causing this? I'm certain it must be something simple and I'm just missing it...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论