0c-35-Automatic release tank use note

Source: Internet
Author: User

1, autorelease use note1) is not placed in the auto-release pool and is automatically added to the auto-release pool1.1because the Autorelease method is not called, the object is not joined to the auto-free pool.intMain () {@autoreleasepool {Student*s =[[Student alloc] init]; [s release]; //Normal Release }return 0;}1.2The external send autorelease in the auto-free pool will not be added to the auto-release pool. intMain () {@autoreleasepool {}//send the object of the Autorelease message to the outside of the brake release tank//cannot be automatically released at this timeStudent *s =[[[ Student alloc] init] autorelease];return 0;}1.3regardless of whether the object was created in the auto-release dominant, or automatically released from the pool, as long as 1 [s autorelease];s are written in the auto-release pool, they are placed in the auto-release pool, note: Autorelease is a method and is only valid if used in an auto-free pool. intMain () {//whether it is created internally or externally in the auto-release poolStudent *s =[[Student alloc] init]; @autoreleasepool {[s autorelease];//when s joins the release pool } return 0;}2The automatic release pool nesting uses the auto-free pool is the stack structure. Stack: Advanced back out. Last in, first out,

intMainintargcConst Char*argv[]) {@autoreleasepool {//The first pool, the 1 students who created noStudent *s =[[[ Student alloc] init] autorelease]; S.No=1; @autoreleasepool {//second pool, inside the student who created NO2Student *S2 =[[[ Student alloc] init] autorelease]; S2.no=2; @autoreleasepool {//second pool, inside the student who created NO3Student *S3 =[[[ Student alloc] init] autorelease]; S3.no=3; }    }}return 0;}
release order: S3,S2,S13automatically frees objects in the pool that are not suitable to occupy large memory space1>try to avoid using this method for large memory, and for this delayed release mechanism, try to use less2>do not put a lot of cyclic operations into the same 1 auto-release pool, which will cause a spike in memory. 2, autorelease error usage1, successive calls to multiple autorelease. @autoreleasepool {Student*s = [[[Student alloc] autorelease] autorelease];//called two times Autorelease, the object is over-disposed.   }2, the object is created outside the release pool, but after the Autorelease is released within the release pool, release is released. intMainintargcConst Char*argv[]) {Student*s =[[Student alloc] init]; @autoreleasepool {[s autorelease];//when the pool is out, the object can be released.} [s release];//An error occurs when the object is freed and then called again.     return 0; }3, Alloc calls the Autorelease, and then calls release. intMainintargcConst Char*argv[]) {@autoreleasepool {Student*s =[[[ Student alloc] init] autorelease];     [s release]; }    return 0;}4, Alloc, and then call release. intMainintargcConst Char*argv[]) {@autoreleasepool {//because release has no return value, this call is wrong. Student *s =[[ [[Student alloc] init] release]; }  return 0;}

0c-35-Automatic release tank use note

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.