Source Address: http://fann.im/blog/2013/04/17/retain-cycle-in-blocks/
Personal notes, which may have a thorough and inaccurate understanding. @fannheyward
Objective-c is based on the reference count (Retaincount) to do memory management, ClassA used to ClassB time, through alloc/retain/copy, etc. will objectb.retaincount+1, when not required by Release/autorelease will objectb.retaincount-1. The object OBJECTB is released after the Retaincount is zeroed. If Objecta retain OBJECTB,OBJECTB in turn retain objecta, the result is both retaincount can not be zero, there is no way to be released, causing memory leaks. This is Retain Cycle.
In general, attention to avoid the two objects retain with each other will not appear retain cycle, but in the use of Blocks should be careful, it is easy to cause retain cycle. This is because Blocks automatically retain the object it refers to (the object in the block), causing the retain Cycle to be slightly inattentive. Documentation: Object and Block Variables:
When a block was copied, it creates strong references to object variables used within the block. If you use a block within the implementation of a method:
- If You access an instance variable by reference, a strong reference are made to self;
- If You access an instance variable by value, a strong reference are made to the variable.
To override this behavior for a particular object variable, you can mark it with the __block storage type Modifie R.
Mrc
For a simple example, Xcode will report Retain Cycle warning:
Uiimageview*Imgview=[[UiimageviewAlloc]initWithFrame:rect]; [imgview setimagewithurl:url Completed:^ (uiimage * imagenserror *error sdimagecachetype cachetype) { Span class= "C1" >//... imgview. Image = image//warning:capturing ' Imgview ' strongly in this block are likely to leads to a retain Cycle}];
Block is also an object, [Imgview setimagewithurl:completed:] Time retain this block, and block and automatically retain Imgview, so it caused the retain Cycle. The workaround is __block to tell the block not to retain the referenced object:
__blockUiimageview*Imgview=[[Uiimageviewalloc initwithframe:rect; [imgview setimagewithurl:url Span class= "NL" >completed:^ (uiimage *imagenserror * errorsdimagecachetype cachetype ) {//... imgview. Image = image;}];
In another case, the object referenced in block is self or self.property, and the workaround is the same:
__blockMyClass*MyClass=Self;Operation.Completeblock=^(NsintegerIndex){[MyClassDoOther];};Self.Imgview=[[UiimageviewAlloc]initWithFrame:Rect];__blockUiimageview *tmpview = _imgview; [_imgview setimagewithurl:URL completed:^(UIImage *image , nserror *error, sdimagecachetype cachetype) { Tmpview. Image = image;}]; }< /c15>
ARC
Cannot use under ARC, __block keywords, instead of __weak or __unsafe_ Unretained . where __weak iOS 5+ only, __unsafe_unretained supports iOS 4. If the App does not need to consider 4.x with __weak will be better, __weak Decorated objects will be set to nil after release, and __ unsafe_unretained will continue to point to the original memory.
__block MyClass *myClass = self; // MRC__weak MyClass *myClass = self; // ARC & iOS 5+__unsafe_unretained MyClass *myClass = self; // ARC & iOS 4.
Some reference articles:
- IOS blocks-three will cause retain cycle anti patterns
- Friday q&a 2010-04-30:dealing with Retain Cycles
- ASIHTTPRequest Using Blocks
- Arc-the meaning of __unsafe_unretained?
- Block and retain cycle in iOS
IOS 8: "Go" block circular reference