Memory NSTimer内存泄漏?

Memory NSTimer内存泄漏?,memory,memory-leaks,nstimer,Memory,Memory Leaks,Nstimer,我正在开发一个应用程序,它基本上运行良好。但显然还不够好。当运行“泄漏”仪器时,我看到我的NSTimer有很多泄漏 NSTimer通过UISwitch启动和停止。每次将开关拨到“OFF”时都会发生内存泄漏 这可能是什么原因造成的?我确信它一定很简单,我只是错过了它…从您在这里展示的内容来看,没有泄漏。不过,有一个编程错误:去掉[self.autoTimer release]。唯一能保护你免于崩溃的是,你已经在前一行将财产归零了。关于泄漏:是否可能是您也从其他地方访问了自动定时器属性?像myPla

我正在开发一个应用程序,它基本上运行良好。但显然还不够好。当运行“泄漏”仪器时,我看到我的NSTimer有很多泄漏

NSTimer通过UISwitch启动和停止。每次将开关拨到“OFF”时都会发生内存泄漏


这可能是什么原因造成的?我确信它一定很简单,我只是错过了它…

从您在这里展示的内容来看,没有泄漏。不过,有一个编程错误:去掉
[self.autoTimer release]。唯一能保护你免于崩溃的是,你已经在前一行将财产归零了。关于泄漏:是否可能是您也从其他地方访问了
自动定时器
属性?像
myPlayView.autoTimer=nil或类似的东西?这完全可以解释这种泄漏,因为计时器仍将在runloop上调度(并因此保留),以确保在更改为其他视图/视图控制器并释放此视图/视图控制器时释放NSTimer。
//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
}