[英]Why do I have a possible retain cycle warning
I have the following block declared in a .h 我在.h中声明了以下块
@property (strong, nonatomic) void(^setHandedness)(BOOL hand);
it is defined in the matching .m 它在匹配的.m中定义
setHandedness = ^(BOOL hand){
_isRightHanded = hand;
};
and is passes to a child view controller which has the same form of block declared. 并传递给子视图控制器,该子视图控制器具有与声明的块相同的形式。 Except with weak 除了弱
@property (weak, nonatomic) void(^setHandedness)(BOOL hand);
Replacing strong with weak removes the warning. 以强代替弱消除警告。 But I don't understand why? 但是我不明白为什么?
the block is then called in the child view controller 然后在子视图控制器中调用该块
setHandedness(handedness);
I have a warning telling me self is likely to cause a retain cycle? 我有一条警告告诉我自我可能会导致保留周期? any ideas. 有任何想法吗。 Cheers. 干杯。
The reason for the warning is this: 警告的原因是这样的:
Now you have two objects referencing each other. 现在,您有两个互相引用的对象。 Even if no one else references them they will keep each other alive and never be deallocated. 即使没有其他人引用它们,它们也将彼此保持生命,并且永远不会被释放。
Here is a quote from Apple's documentation for blocks and variables 这是Apple文档中有关块和变量的引文
If you access an instance variable by reference, self is retained; 如果通过引用访问实例变量,则self会保留;
Your second view controller has nothing to do with your retain cycle. 您的第二个视图控制器与您的保留周期无关。
You have a possible retain cycle because it is usually the case that when the parent points to the child with a strong pointer, and the child points to the parent with another strong pointer, they will keep themselves alive and will never get dealocated (thats how ARC works). 您可能会有一个保留周期,因为通常情况下,当父对象使用强指针指向子对象,而子对象使用另一个强指针指向父对象时,它们将保持生命,并且永远不会消失(这就是为什么ARC)。 When you replace the child pointer for a weak one then this will not happen. 当您将子指针替换为弱指针时,则不会发生这种情况。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.