Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

第39题的一些疑问 #138

Open
jhz-libowen opened this issue Jul 6, 2021 · 2 comments
Open

第39题的一些疑问 #138

jhz-libowen opened this issue Jul 6, 2021 · 2 comments

Comments

@jhz-libowen
Copy link

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

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

@ChenYilong
Copy link
Owner

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

ChenYilong added a commit that referenced this issue Aug 30, 2021
@ChenYilong
Copy link
Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants