First,* Core Foundation Exchange with objective-c object *
The arc management mechanisms that are required for the core foundation to be exchanged with Objective-cobject are:
(1) (__bridge_transfer) op or alternatively Cfbridgingrelease (OP) is used to consume a retain-count of a cftyperef while T Ransferring it over to ARC. This could also is represented by
id someObj =(__bridge <NSType>) op; CFRelease(op);
(2) (__bridge_retained) op or alternatively Cfbridgingretain (OP) is used to hand a nsobject over to Cf-land while giving It a +1 retain count. You should handle a cftyperef to create this is the same as you would handle a result of cfstringcreatecopy (). This could also is represented by
CFRetain((__bridge CFType)op); CFTypeRef someTypeRef =(__bridge CFType)op;
(3) __bridge just casts between Pointer-land and Objective-c Object-land. If you have no inclination to use the conversions above, use this one.
II.* Arc and IOS4 *
Arc in IOS4 is not __weak keyword, need to use unsafe_unretained to replace.
Iii.* Memory leaks in ARC *
The use of Arc does not mean that there is no memory leak in our project. Under the ARC mechanism, the most common cause of memory leaks is a cyclic strong reference. The easy-to-appear occasions are:
①outlet Type pointer
Pointer variables of type outlet should be declared with the weak property
② delegate
Be sure to set the delegate property to weak, why I don't explain, I don't understand, please hit here
③block
The following code is not problematic under the MRC condition:
MyViewController * __block myController = [[MyViewController alloc] init…]; ... myController.completionHandler = ^(NSInteger result) { [myController dismissViewControllerAnimated:YES completion:nil];
However, in the arc condition, the memory leaks, causing the object that the Mycontroller points to not be freed.
The reason is that when the variable x declared by the __block ID x is used in a block, the MRC condition does not increase the reference count of X, but in the arc condition it makes the X reference count plus one, so be sure to note that!!!!!!!!!!!!
The above problem code has the following solutions:
Programme one:
MyViewController * __block myController = [[MyViewController alloc] init…]; ... myController.completionHandler = ^(NSInteger result) { [myController dismissViewControllerAnimated:YES completion:nil]; myController = nil;
The simplest solution, when Mycontroller is used in a block, is that it points to nil, and the object that does not have a strong-type pointer to the object that Mycontroller points to is released.
Scenario Two:
MyViewController *myController = [[MyViewController alloc] init…]; ... MyViewController * __weak weakMyViewController = myController; myController.completionHandler = ^(NSInteger result) { [weakMyViewController dismissViewControllerAnimated:YES completion:nil]; };
The scheme uses a temporary __weak-type pointer weakmyviewcontroller, which does not cause a reference count to be added in the block, but has a hidden danger that the operation performed inside the block is invalid when the object is released externally. The following scenario three can solve this problem.
Programme III:
MyViewController *myController = [[MyViewController alloc] init…]; ... MyViewController * __weak weakMyController = myController; myController.completionHandler = ^(NSInteger result) { MyViewController *strongMyController = weakMyController; if (strongMyController) { ... [strongMyController dismissViewControllerAnimated:YES completion:nil]; ... } else { // Probably nothing... } };
That is, before using the Mycontroller object in the block and then declaring a temporary pointer to the strong type, pointing to the weak type pointer, the Strongmycontroller pointer becomes a valid strong reference, and the object it points to can be guaranteed not to be released.
④ Timer
Timers are also places where memory leaks are very easy to generate. For example, the following code
@implementation animatedview { ? Nstimer *timer; ?} - (ID) Initwithcoder: (Nscoder *) Adecoder {? ?if(( Self= [SuperInitwithcoder:adecoder]) {timer = [Nstimer scheduledtimerwitht imeinterval:0.1Target SelfSelector@selector(Handletimer:) UserInfo:NilRepeatsYES]; }return Self; } - (void) Dealloc {[Timer invalidate]; } - (void) Handletimer: (nstimer*) Timer {//do something}
At first glance this code is not a problem, but run up to find that the Dealloc method is not called, Self has a strong reference to a timer, the timer has a self strong reference, typical circular reference!
The workaround is to set the timer's property to __weak.
Iv.* @autoreleasepool and NSAutoreleasePool *
The use of NSAutoreleasePool is not supported in arc, but @autoreleasepool can be used instead. @autoreleasepool can be used in an arc environment or in a non-arc environment, and is more efficient than the former, as described in Apple's official website:
anof
V.* NEW rules to be followed for use with arc *
① do not call [Super Dealloc] in the Dealloc method;
② cannot use Retain/release/retaincount/autorelease
③ cannot use Nsallocateobject/nsdeallocateobject
④ cannot use Nszone
⑤objective-c object cannot be a member of the C language struct (struct/union)
Reference Links:
-
ARC & MRC This is not a technical route issue.
-
OBJECTIVE-C memory Management MRC vs. arc
-
Arc usage Experience (ii)
-
Performance Comparison:cocos2d-iphone v2 vs v3 vs Sparrow and ARC vs MRC
- The development of the iphone in Layman's (4)-arc cycle Reference
- Transitioning to ARC Release Notes
IOS arc VS MRC Learning notes