NSTimer retain了它的target

今天一直在释放一个类对象B(这个类中有一个timer),根据arc的原理来说, B的retainCount为0时,就会调用dealloc。但是当为我把B=nil,没有进入dealloc。

-(void)dealloc

{

  [timer invalidate];

timer = nil;

}

后来经过朋友提醒知道,nstimer对B类有引用,也就是在初始化时对target有retain.

timer = [NSTimer scheduledTimerWithTimeInterval:1.0 self selector:@selector( logout: ) userInfo:nil repeats:YES];

后来把self改为__weak,发现也不行。

__weak id weakSelf = self;

timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:weakSelf selector:@selector( logout: ) userInfo:nil repeats:YES];

最终在stackoverflow里面找到了一种使用代理的方法。

http://stackoverflow.com/questions/16821736/weak-reference-to-nstimer-target-to-prevent-retain-cycle

单独建立一个WeakTimerTarget

@interface WeakTimerTarget : NSObject

{

__weak id target;

SEL selector;

}

-(id)initWithTarget:(id)tg selector:(SEL)sel;

- (void)timerDidFire:(NSTimer *)timer;

@end

@implementation WeakTimerTarget

-(id)initWithTarget:(id)tg selector:(SEL)sel

{

self     =  [super init];

target   = tg;

selector = sel;

return self;

}

- (void)timerDidFire:(NSTimer *)timer

{

if(target)

{

[target performSelector:selector withObject:timer];

}

else

{

[timer invalidate];

}

}

@end

在B类中这样使用:

WeakTimerTarget *weakTarget = [[WeakTimerTarget alloc] initWithTarget:self selector:@selector(logout:)];

[NSTimer scheduledTimerWithTimeInterval:1.0 target:weakTarget selector:@selector( timerDidFire: ) userInfo:nil repeats:YES];

这样就可以解决不能释放内存的问题了,但是为什么使用__weak self 和 __strong self效果是一样的还不知道为什么,希望大家指教。

时间: 2024-12-12 12:23:39

NSTimer retain了它的target的相关文章

防止 NSTimer retain 作为 target 的 self

先吐槽一下这个标题,空格略蛋疼,不像中文,但是不写空格看上去则更诡异,求解决方案…… NSTimer会retain它的target,这样如果在控制器当中定义一个NSTimer,target指定为self,则会引起循环引用. 解决方案和防止block引用self一样,第一步需要把NSTimer的操作封装到一个block里,第二步则需要传递一个self的弱引用给block. 首先定义一个NSTimer的分类: 1 #import <Foundation/Foundation.h> 2 3 @int

iOS之 NSTimer

以前没怎么了解过这个NSTimer,其实还是有挺多坑的,今天来总结一下: 首先我们一起来看这个: 我在A  -> (push) -> B控制器,然后再B控制器中开启了一个NSTimer.然后我又pop到A pop到A的时候,定时器还在运行,并且B没有被释放(未调用dealloc).why? 这就不得不让我联想到我上篇写到的 “常驻线程”了,莫非NSTimer也是添加到了RunLoop? 这说明本例中的NSTimer确实是跑在了NSRunLoop中. 那为什么B没有释放呢? Timer对Targ

IOS 中NSTimer使用注意事项

1.初始化 + (NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)yesOrNo; + (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelec

【整理】NSTimer使用及注意事项

1.NSTimer的创建 // 创建一个定时器,但是么有添加到运行循环,我们需要在创建定时器后手动的调用 NSRunLoop 对象的 addTimer:forMode: 方法. + (NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti invocation:(NSInvocation *)invocation repeats:(BOOL)yesOrNo; + (NSTimer *)timerWithTimeInterval:(NSTimeInte

【转】iOS中定时器NSTimer的使用

原文网址:http://www.cnblogs.com/zhulin/archive/2012/02/02/2335866.html 1.初始化 + (NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)yesOrNo; + (NSTimer *)scheduledTimerWithTime

【转】IOS 计时器 NSTimer

原文网址:http://blog.csdn.net/tangshoulin/article/details/7644124 1.初始化 + (NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)yesOrNo; + (NSTimer *)scheduledTimerWithTimeInter

iOS 里面 NSTimer 防止 循环引用

使用NSTimer的类 #import "TBTimerTestObject.h" #import "TBWeakTimerTarget.h" @interface TBTimerTestObject() @property (nonatomic, weak) NSTimer *timer; @end @implementation TBTimerTestObject - (void)dealloc { NSLog(@"timer is dealloc&q

iOS定时器NSTimer的使用方法

1.初始化 + (NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)yesOrNo; + (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelec

NSTimer 详解

NSTimer的使用方法 1.初始化 + (NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)yesOrNo; + (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selecto