(reprint) OC Study---Circular citation problem

Source: Internet
Author: User

in a previous article, we introduced the reference problem of array manipulation objects and the concept of an auto-release pool. today we continue to look at a pain in the reference count: Circular reference

On the issue of circular references, there is not much explanation here, that is, multiple objects are referenced to each other, forming loops.

Take a look at a specific example: reference to each other between the dog class and the person class

Dog.h

1 //  2 //Dog.h3 //29_cyclepointer4 //  5 //Created by Jiangwei on 14-10-13. 6 //Copyright (c) 2014 Jiangwei.  All rights reserved. 7 //  8   9 #import<Foundation/Foundation.h>Ten    One #import "Person.h"   A    - @interfaceDog:nsobject -    the //There is no retain here, and if you use retain, it will form a circular reference . -@property (nonatomic,assign,readwrite) person *Person ;  -    -- (void) Dealloc;  +    - @end

Dog.m

1 //  2 //DOG.M3 //29_cyclepointer4 //  5 //Created by Jiangwei on 14-10-13. 6 //Copyright (c) 2014 Jiangwei.  All rights reserved. 7 //  8   9 #import "Dog.h"  Ten    One @implementationDog A    -- (void) dealloc{ -     //[_person release];  theNSLog (@"Dog Dealloc");  - [Super Dealloc];  - }   -    + @end

A property in the dog class that has a person type

Person.h

1 //  2 //Person.h3 //29_cyclepointer4 //  5 //Created by Jiangwei on 14-10-13. 6 //Copyright (c) 2014 Jiangwei.  All rights reserved. 7 //  8   9 #import<Foundation/Foundation.h>Ten    One @classDog;  A    - @interfacePerson:nsobject -    the@property (nonatomic,retain,readwrite) Dog *Dog;  -    -- (void) Dealloc;  -    + @end

Person.m

1 //  2 //person.m3 //29_cyclepointer4 //  5 //Created by Jiangwei on 14-10-13. 6 //Copyright (c) 2014 Jiangwei.  All rights reserved. 7 //  8   9 #import "Person.h"  Ten    One #import "Dog.h"   A    - @implementation Person -    the- (void) dealloc{ - [_dog release];  -NSLog (@"Person dealloc");  - [Super Dealloc];  + }   -    + @end  

A property with the dog type in the person class

Check the test code.

Main.m

1 //  2 //main.m3 //29_cyclepointer4 //  5 //Created by Jiangwei on 14-10-13. 6 //Copyright (c) 2014 Jiangwei.  All rights reserved. 7 //  8   9 #import<Foundation/Foundation.h>Ten    One #import "Dog.h"   A #import "Person.h"   -    - //Circular References the //is a very troublesome thing, completely by experience - intMainintargcConstCharchar *argv[]) {   -        -Person *p =[[Person alloc] init];  +Dog *dog =[[Dog alloc] init];  -        +[P Setdog:dog];//Dog Count: 2 A        at[Dog Setperson:p];//Person count: 2 -        -[P release];//Person count: 1 -[Dog release];//Dog Count: 1 -        -     //the reason for not releasing is that the Dealloc method is not executed, and the release code is not executed, and the dog and person are waiting each other to form a ring. in     //the solution version is to sever the connection between them. -     //do not use retain in @property, use Assgin to        +NSLog (@" is Over");  -        the     return 0;  *}

We define a person object and a dog object separately, and then reference each other, but when we call their release method, the two objects are not freed.

The reason is simple: both the person and the dog refer to each other, and when the release method is executed, the reference count is still 1, so the Dealloc method is not called. the Dealloc method is not executed, and the release code is not executed, and thedog and the person are waiting each other to form a ring.

the solution is to cut off the connection between them: when defining attributes in a party, the retain is not used in the @property, and assgin is used. The release method is no longer called in the Dealloc method.

In the example above, we can see that the assgin is used in the dog class.

Summarize

Circular reference is the biggest problem encountered when object is destroyed, in Java, the garbage collector will also encounter such problems, so do not use reference counting method to manage objects, but another way to manage.

(reprint) OC Study---Circular citation problem

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.