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

Source: Internet
Author: User

Self-release Pool @autorelease interview frequency may be the release is also high.


(1) After you alloc an object (such as P1) in the active release Pool @autoreleasepool{}. [P1 autorelease] is still required, but this statement is different from [P1 release]; The latter represents the P1 of the retainCount-1, while the former only means to put P1 into their own active release pool returned to a self, the active release of the pool to end the destruction, unified on the inside of the object reference count RetainCount-1.


(2) @autoreleasepool {} can be created arbitrarily. can also be nested for use.


(3) Whether the object is created within its own active release pool or outside, simply write a [P1 autorelease] in its own active release Pool;p 1 will be placed in its own active release pool.

Note that Autorelease is a method. It only works if it is used in its own active release pool.


(4) Assume that an object is added repeatedly to its own active release pool such as [P1 autorelease]; [P1 autorelease];. Then there will be an error. The reason is: load several times. Then release the pool on their own initiative will use [P1 release]; released several times, but because these two loaded objects are in fact an object of the same address, so the first time self-release is correct, the second time you voluntarily release the discovery has been released. So p1 becomes a wild pointer.


(5) The following is the use of self-release pool nesting rules and points of attention.

#import <Foundation/Foundation.h> #import "Person.h" int main (int argc, const char * argv[]) {person    *p1=[[ Person Alloc]init];    @autoreleasepool {        @autoreleasepool {            [P1 autorelease];        } When running here, P1 is voluntarily released by itself    }        //The following code has an error    @autoreleasepool {        [P1 autorelease];//at this time P1 is added in        @ Autoreleasepool {            [P1 autorelease];//is repeatedly loaded in.] But still the same        }//here, P1 was released on his own initiative. So the one that came in the first time was also released. Because it is the same object    }//this is in call [P1 release]; An error occurred: The wild pointer        return 0;}

(6) Application of @autoreleasepool: Suppose you need to create an object in a method. With this object as the return value, you can use [autorelease] in this method, and add it to your own active release pool. Otherwise. directly with [* * * release], to match the alloc words, in this method has already put this object alloc and release of the equivalent of released. Then the so-called return object returns a wild pointer (not pointing to whatever object). Of course. The code page that calls this method needs to be written in the scope of its own active release pool before it takes effect.


(7) Connect the top. In that method of returning an object, creating the object does not recommend using the class name directly, but using self. Otherwise, a subclass call is assumed to crash.

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 replacing the retain with strong before manually managing memory. Although the latter can still be used. Since our memory management in arc is to see if there is a strong pointer to the object, if there is no recycling. If not, recycle.

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, it is very rare to convert the entire non-arc into arc. Suppose we import a third-party library. Requires non-arc and arc coexistence. That is, our system is arc by default. We need to get the system out of the way. Non-ARC third-party libraries, such as the following settings: Double-click the. m file for the response. Enter-fno-objc-arc to return.



(11) by the way. When there are two classes of circular references (that is, a should include B. b to include a. That is, a object to be a variable B, b object as a variable), only need to change one side of the strong to weak. And in the responding. h file, change the #import ". h" to class * * *.

Assuming that you cannot use the method of that class because you changed to class * * *, you simply need to #import the ". h" file in its. m file, which does not conflict because it is not imported in the. h file.

"iOS Dev-34" self-release pool @autoreleasepool usage precautions 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.