iOS Review Note 7: Circular reference issues

Source: Internet
Author: User

1 @class
@class ClassName, just tell the editor that ClassName is a class


Avoid circular copying
Using the @class declaration in. h
Import header files in. m files


2 Circular Reference problems
2.1 Definition:
Refers to two objects retain each other, through release is unable to destroy the two objects

2.2 Description:
For example, two classes of objects A and B are created in the main function, and the reference count is now 1.
Now let A and B refer to each other, and the reference count of two objects increases by 1 and becomes 2.
execution [A release]; The [B release];main function releases its own object, but a and B reference each other, and the reference count is still 1.
At this point A and B will not be released, because to release a you must first release B, and then release a in the Dealloc method of B.
Similarly, to release B, you must first release a, and then release B in the Dealloc method of a.
So these two objects will always be in memory without releasing, which is the circular reference problem.


2.3 Solutions
In general, you can set the reference property of a short life cycle to assign. Or a, B are set to assign


Examples of circular references:
Person.h@class car@interface Person@property (noatomic, retain) car* Car; @end

Person.m#import "Person.h" @implementation person-(void) Dealloc{[_car Release];_car = nil;[ Super Dealloc];} @end


Car.h@class person@interface Car@property (noatomic, retain) person* person; @end


Car.m#import "Car.h" @implementation car-(void) Dealloc{[_person release];[ Super Dealloc];} @end


Main.m#import "Car.h" #import "Person.h" int main () {person* person = [[Person alloc] init];//person 1car* Car = [[Car al] LOC] init];//Car 1person.car = car;//car 2car.person = person;//person 2[car release];//car 1[person release];//Perso n 1return 0;}



Workaround Example:
Car.h@class person@interface Car@property (noatomic, assign) person* person; @end



Car.m#import "Car.h" @implementation car-(void) dealloc{[super dealloc];} @end


Main.m#import "Car.h" #import "Person.h" int main () {person* person = [[Person alloc] init];//person 1car* Car = [[Car al] LOC] init];//Car 1person.car = car;//car 2car.person = person;//person 1[car release];//car 1[person release];//Perso n 0,car 0return 0;}



iOS Review Note 7: Circular reference issues

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.