The---Circular reference problem of OC Learning article

Source: Internet
Author: User

In a previous article, we introduced the reference problem of array manipulation objects and the concept of the automatic release pool:

http://blog.csdn.net/jiangwei0910410003/article/details/41926183

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

  dog.h//  29_cyclepointer////  Created by Jiangwei on 14-10-13.//  Copyright (c) 2014 Jiangwei. All rights reserved.//#import <Foundation/Foundation.h> #import "Person.h" @interface dog:nsobject//here without retain, If you use retain, you will form a circular reference @property (nonatomic,assign,readwrite) person *person;-(void) dealloc; @end


DOG.M

  dog.m//  29_cyclepointer////  Created by Jiangwei on 14-10-13.//  Copyright (c) 2014 Jiangwei. All rights reserved.//#import "Dog.h" @implementation dog-(void) dealloc{    //[_person release];    NSLog (@ "dog dealloc");    [Super Dealloc];} @end
A property in the dog class that has a person type


Person.h

  person.h//  29_cyclepointer////  Created by Jiangwei on 14-10-13.//  Copyright (c) 2014 Jiangwei. All rights reserved.//#import <Foundation/Foundation.h> @class Dog; @interface Person:nsobject@property ( Nonatomic,retain,readwrite) Dog *dog;-(void) dealloc; @end

person.m

  person.m//  29_cyclepointer////  Created by Jiangwei on 14-10-13.//  Copyright (c) 2014 Jiangwei. All rights reserved.//#import "Person.h" #import "Dog.h" @implementation person-(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

  main.m//  29_cyclepointer////  Created by Jiangwei on 14-10-13.//  Copyright (c) 2014 Jiangwei. All rights reserved.//#import <Foundation/Foundation.h> #import "Dog.h" #import "Person.h"//circular references//is a very troublesome thing, Completely by experience int main (int argc, const char * argv[]) {person        *p = [[Person alloc] init];    Dog *dog = [[Dog alloc] init];        [P Setdog:dog];//dog Count: 2        [Dog Setperson:p];//person count: 2        [P release];//person count: 1    [Dog release];// Dog count: 1        //is not released because the Dealloc method is not executed, the inside of the release code is not executed, the dog and the person are waiting each other, forming a ring    //Resolution version is to cut off their connection    //@ The property does not use retain, using Assgin        NSLog (@ "is over");        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 released.

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, do not use retain in the @property, use assgin

At the same time, 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 object of the destruction of the biggest problem encountered 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, you can refer to: http://blog.csdn.net/ jiangwei0910410003/article/details/40709457







The---Circular reference problem of OC Learning article

Related Article

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.