iOSInterviewQuestions icon indicating copy to clipboard operation
iOSInterviewQuestions copied to clipboard

第39题的一些疑问

Open jhz-libowen opened this issue 4 years ago • 2 comments

这个因为 [NSOperationQueue mainQueue] 并非单例,这个写法非常像单例,然后我运行下,发现不管在什么情况下 [NSOperationQueue mainQueue] 返回的地址都是相同的进一步证明了我的猜想,让后我下载了Gnustep源码,参考了下[NSOperationQueue mainQueue]和[NSNotificationCenter defaultCenter]写法一样都是单例,那么怎么确认是否有循环引用呢, 我参考了下NSNotificationCenter源码 screenshot screenshot screenshot 这就证明了 screenshot 然后参考addOperationWithBlock源码 screenshot screenshot 所以[NSOperationQueue mainQueue]虽然是单例,但是不持有block,不会造成循环引用,传递完成就销毁了,不会造成无法释放的内存泄漏问题。

还有的问题 screenshot 我用instrments 无法复现这张图,希望作者能帮我看看。

jhz-libowen avatar Jul 06 '21 13:07 jhz-libowen

赞, @jhz-libowen 文档已经更新勘误,并引用到本issue地址

ChenYilong avatar Aug 30 '21 16:08 ChenYilong

对于情况❸ NSNotificationCenterIVARBlock,我之前的分析也有误,_observer并不会持有block,而是由NSNotificationCenter持有。因此,这里同样没有形成循环引用,但是如果没有在合适的时机移除监听,将会导致内存泄漏。 文档已经更新 https://github.com/ChenYilong/iOSInterviewQuestions/commit/8a2daf0a97d05f7db8441cef7af9474b2aab3b4e

ChenYilong avatar Jul 27 '23 19:07 ChenYilong