IOS arc VS MRC Learning notes

Source: Internet
Author: User

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:

    1. ARC & MRC This is not a technical route issue.

    2. OBJECTIVE-C memory Management MRC vs. arc

    3. Arc usage Experience (ii)

    4. Performance Comparison:cocos2d-iphone v2 vs v3 vs Sparrow and ARC vs MRC

    5. The development of the iphone in Layman's (4)-arc cycle Reference
    6. Transitioning to ARC Release Notes

IOS arc VS MRC Learning notes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.