Will self retain within block?

╄→гoц情女王★ 提交于 2019-12-06 09:14:53

First, retainCount is useless. Don't call it..

Blocks only retain captured objects when the block is copied. Thus, self won't be retained by the block in that example.

OK I did some research, now things became more clear. firstly, I didn't use @property on block1, which means when I set it, nothing is copied, so they are not retained, secondly, if we do a [block copy], the variables will be retained, if we dont copy, the block points to a stack address, copy it to heap to make it safe.

the variable 'array' is a Member variable, so it's not retained and meanwhile the self will be retained, whether you put it in the block or not, if the variable is a local variable, it will be retained. ( this is the thing that Im still confused abt, why the member variable is not retained,instead the self is added one more on retain count??? pls answer me?)

after using the block we could set it to nil self.block = nil; to make variables released, and avoid the retain cycle.

PS. a method to break retain cycle is use __block id weakSelf = self; in the block, so it means __block variables are also not retained.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!