NSTimer潛在的內(nèi)存泄露問(wèn)題

對(duì)于NSTimer使用大家肯定都不陌生,貼一張使用代碼

? ? ? ? 看代碼中SecondViewController有一個(gè)屬性是testTimer,然后在[self test01]中給self.testTimer賦值,并進(jìn)行了強(qiáng)引用,然后我們?cè)诳捶椒╗NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(doSomeThing) userInfo:nil repeats:YES],里面的target:所傳參數(shù)為self,SecondViewController,即所以SecondViewController與self.testTimer之間存在循環(huán)引用,會(huì)造成內(nèi)存泄漏。


解決辦法:

? ? ? ? 我們發(fā)現(xiàn)testTimer是用strong修飾的,想到之前我們解決delegate循環(huán)引用是,對(duì)delegate屬性使用weak修飾來(lái)打破閉環(huán)解決循環(huán)引用問(wèn)題,那么這里testTime也使用weak修飾是否能夠解決呢?經(jīng)過(guò)測(cè)試發(fā)現(xiàn)無(wú)法解決,那問(wèn)題出現(xiàn)在哪里?

先看這兩個(gè)創(chuàng)建NSTimer對(duì)象的方法:

1. + (NSTimer*)timerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)yesOrNo;//生成timer但不執(zhí)行

2. + (NSTimer*)scheduledTimerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)yesOrNo;//生成timer并且納入當(dāng)前線程的run loop來(lái)執(zhí)行,不需要用addTimer方法。

分析:

self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(doSomeThing) userInfo:nil repeats:YES];
? ? //相當(dāng)于
? ? self.timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(doSomeThing) userInfo:nil repeats:YES];
? ? [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSDefaultRunLoopMode];

? ? ? 開(kāi)啟一個(gè)NSTimer實(shí)質(zhì)上是在當(dāng)前的runloop中注冊(cè)了一個(gè)新的事件源, 所以會(huì)發(fā)現(xiàn)NSRunLoop也對(duì)timer進(jìn)行了強(qiáng)引用。看下面示意圖(假設(shè)testTimer屬性用weak修飾)

所以timer引用計(jì)數(shù)永遠(yuǎn)也不會(huì)變成0,那么也就不會(huì)銷(xiāo)毀,這樣也就導(dǎo)致了對(duì)SecondViewController的引用也不會(huì)斷開(kāi),那么SecondViewController也不會(huì)銷(xiāo)毀了。

蘋(píng)果給NSTimer類(lèi)提供的有一個(gè)對(duì)象方法

- (void)invalidate; //銷(xiāo)毀定時(shí)器的方法,詳解:將timer從runloop上移除的唯一方法,同時(shí)timer也會(huì)移除target目標(biāo)和userinfo參數(shù)對(duì)象的強(qiáng)引用關(guān)系

Stops the timer from ever firing again and requests its removal from its run loop.

This method is the only way to remove a timer from an NSRunLoop object. The NSRunLoop object removes its strong reference to the timer, either just before the invalidate method returns or at some later point.

If it was configured with target and user info objects, the receiver removes its strong references to those objects as well.


問(wèn)題: 那么定時(shí)器timer屬性是用strong修飾還是用weak修飾?

答:都可以,如果用strong修飾的話,調(diào)用- (void)invalidate方法銷(xiāo)毀定時(shí)器之后,還有調(diào)用self.timer = nil來(lái)移除控制器對(duì)定時(shí)器的強(qiáng)引用,如果使用weak就不需要了。建議使用strong,strong的性能還是要更優(yōu)于weak。


擴(kuò)展:上面所說(shuō)的調(diào)用- (void)invalidate方法來(lái)銷(xiāo)毀定時(shí)器肯定是能夠解決內(nèi)存泄漏問(wèn)題的,但對(duì)于這種方式還是可以優(yōu)化,因?yàn)槟銦o(wú)法保證開(kāi)發(fā)者一定會(huì)在需要的地方調(diào)用了銷(xiāo)毀的方法,那么有什么好的優(yōu)化方案呢?接下來(lái)介紹:

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容