0c-33-@class, cyclic retain

Source: Internet
Author: User

2 objects have references to each other, and a in B,b has a.

In the case of cyclic retain, the object cannot be freed, so only one party can use the Retain,retain for the object assign for the base type, and a reference to cat when assign does not add 1 to the Cat object counter.

Cat.h #import <Foundation/Foundation.h>@class  Girl; @interface  *name; @property (nonatomic,assign)int*owner; @end
CAT.M#import "Cat.h"@implementationCat- (void) dealloc{NSLog (@"The cat was released .");    [_owner release]; [Super Dealloc];}@endGirl.h#import<Foundation/Foundation.h>@classCat;//@class Cat;//just tell the compiler that cat is a cat class, and you can declare an object.//Recommendation: Later, when the header file introduces a class, it is recommended to use @class//Aimporb BIMPORTC CIMPOTD: Very low efficiency.@interfaceGirl:nsobject//In the case of cyclic retain, the object cannot be freed, and a reference to cat will not add 1 to the Cat object counter at this time only when a party uses assign,assign@property (nonatomic,assign) Cat *Cat;@endGIRL.M#import "Girl.h"@implementationGirl-(void) dealloc{NSLog (@"Girl was released");    [_cat release]; [Super Dealloc];}@endmain.m#import<Foundation/Foundation.h>#import "Girl.h"#import "Cat.h"intMainintargcConst Char*argv[]) {@autoreleasepool {Girl*g =[[Girl alloc] init]; Cat*c =[[Cat alloc] init]; G.cat= C;//The girl made a retain,c to the cat. The object counter does not add 1, C is treated as a basic type, c=1C.owner = g;//The cat had a retain,g=2 with the girl.[g release];//g=1[C release];//c=1    }    return 0;}

/**/

[email protected], loop retain

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.