"iOS Dev-34" Automatic release pool @autoreleasepool usage considerations and ARC mechanism-interview required content

Source: Internet
Author: User

Auto-free Pool @autorelease interview frequency may be the release is also higher.


(1) After alloc an object (such as P1) in the auto-release pool @autoreleasepool{}, it is still necessary to use [P1 autorelease]; Except this statement and [P1 release]; the latter represents P1 retainCount-1, The former simply means putting the P1 into the auto-release pool and returning a self, and automatically releasing the pool at the end of the destroy, unifying the object reference count on the inside retainCount-1.


(2) @autoreleasepool {} can be created arbitrarily or nested.


(3) Whether this object is created within or outside of the auto-release pool, simply write a [P1 autorelease] in the auto-release pool;p 1 will be placed in the auto-release pool. Note Autorelease is a method and is only valid if used in an auto-free pool.


(4) If an object is added repeatedly to the automatic release pool such as [P1 autorelease]; [P1 autorelease];, then there will be an error. The reason is: load several times, then automatically release the pool will use [P1 release], release several times, but because these two loaded object is actually an object the same address, so the first time automatically released correctly, the second automatic release when the discovery has been released, so P1 becomes a wild pointer.


(5) The following are the usage rules and points of attention for the automatic release pool nesting.

#import <Foundation/Foundation.h> #import "Person.h" int main (int argc, const char * argv[]) {person    *p1=[[ Person Alloc]init];    @autoreleasepool {        @autoreleasepool {            [P1 autorelease];        } When executed here, P1 is automatically released    }        //The following code has an error    @autoreleasepool {        [P1 autorelease];//at this time P1 is added in        Autoreleasepool {            [P1 autorelease];//is repeatedly loaded, but still the same        }//here, p1 is automatically freed, so the first added one is also released because it is the same object    }// So this is in call [P1 release]; An error occurred: The wild pointer        return 0;}

(6) @autoreleasepool application: If you need to create an object in the method, and the object as the return value, then you can use the [* * * * Autorelease] In this method, add it to the automatic release pool, otherwise, directly with [* * release]; To match alloc words, in this method has already put this object Alloc and release again equivalent to release, then the so-called return object returned when a wild pointer (no point to any object). Of course, the code page that calls this method needs to be written in the scope of the auto-release pool before it takes effect.


(7) Connect the top. In that method of returning an object, creating the object is not recommended to use the class name directly, but with self, otherwise the child class call crashes if there is one. such as car *car1=[[self alloc]init];


(8) In fact, such as NSString *str1=[nsstring stringwithformat:@ "%@", @ "Hello"]; it also calls a method and returns a String object. We learned that the stringWithFormat should also have returned a autorelease, as compared to (6) and (7).


(9) In the ARC mechanism, we use the member variables declared by @property, we recommend using Strong instead of the retain when manually managing memory, although the latter can still be used. Because we are in ARC memory management is to see if there is a strong pointer to the object, if there is not recycled, if not on the recycling. So the strong pointer is strong, the opposite is weak. And the basic data types we are accustomed to using assign.


(10) Although Xcode offers non-arc conversion to arc, very few convert the entire non-arc into arc. If we import a third-party library, we need non-arc and arc coexistence, that is, our system is arc by default, we need to let the system do not control this non-arc third-party library, the following settings: Double-click the response of the. m file, enter-fno-objc-arc return.



(11) By the way, when there are two class circular references (that is, a to include b,b to include a, that is, a object to be a variable of B, B object as a variable), only need to change one side of the strong to weak, and in the response of the. h file to change the #import ". h" to class * **。 If you cannot use the method of that class because you changed to class * * *, simply #import the ". h" file in its. m file, which does not conflict because it is not imported in the. h file.

"iOS Dev-34" Automatic release pool @autoreleasepool usage considerations and ARC mechanism-interview required content

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.