Dark Horse programmer-memory management-Set Method Memory Management

Source: Internet
Author: User
Tags manual writing

1. Set Method Memory Management

When an object has the attributes of another object, you need to retain the current object in the set method, because your attributes point to another object, you need to let another object know that a unit is using me.

 

However, if this writing method is not complete enough, new problems may occur. If the object property has already pointed to a certain object, and a new object is passed in, the property pointer points to a new object, and then the new object is retained, however, there is no manual release for the old object at this time. You need to perform the release operation. Another problem is to make a judgment before the old release object and the new object of retain to determine whether all the objects in the current time and the previous objects point to the same object, if yes, you do not need to perform any release and retain operations.

 

The following is the Demo code. Create a person class and a car class. The person class attributes include the car class.

 1 #import <Foundation/Foundation.h> 2 #import "Car.h" 3  4 @interface Person : NSObject 5 { 6     Car *_car; 7     int _age; 8 } 9 10 - (void)setAge:(int)age;11 - (int)age;12 13 - (void)setCar:(Car *)car;14 - (Car *)car;15 16 @end
1 # import "person. H" 2 3 @ implementation person 4-(void) setcar :( car *) Car 5 {6 if (car! = _ Car) 7 {8 // perform a release 9 [_ car release] for the current car (old car); 10 11 // perform a retain operation on the new car 12 _ car = [Car retain]; 13} 14} 15-(car *) car16 {17 return _ car; 18} 19 20-(void) setage :( INT) age21 {// basic data type does not need to be managed memory 22 _ age = age; 23} 24-(INT) age25 {26 return _ age; 27} 28 29-(void) dealloc30 {31 // when a person is absent, indicates that no car is used. 32 // a release operation is performed on the car. 33 [_ car release]; 34 35 nslog (@ "% d-year-old person object is recycled ", _ age); 36 37 [Super dealloc]; 38} 39 40 @ end
 1 #import <Foundation/Foundation.h> 2  3 @interface Car : NSObject 4 { 5     int _speed; 6 } 7  8 - (void)setSpeed:(int)speed; 9 - (int)speed;10 @end
1 # import "car. H "2 3 @ implementation Car 4-(void) setspeed :( INT) speed 5 {6 _ speed = speed; 7} 8-(INT) speed 9 {10 return _ speed; 11} 12 13 14-(void) dealloc15 {16 nslog (@ "Car objects with a speed of % d are recycled", _ speed); 24 25 [Super dealloc]; 26} 27 28 @ end

 

Summary:

1> memory management code specifications:
1. As long as alloc is called, there must be release (autorelease)
2. if the object is not generated through alloc, release is not required.

2> code specification of the Set Method
1. Basic data type: Direct Replication
-(Void) setage :( INT) Age
{
_ Age = age;
}

2. OC object type
-(Void) setcar :( car *) Car
{
// First determine whether the object is a new object
If (car! = _ Car)
{
// Perform a release operation on the old object
[_ Car release];

// Retain the new object
_ Car = [Car retain];
}
}

3> code specification of the dealloc Method
1. Be sure to [Super dealloc] and put it to the end
2. Perform a release for other objects owned by self (current ).


Ii. Property Parameters

The above code is obviously troublesome, And the set method should be written every time according to the format. The property parameter can help us improve the code.

1> Set Method Memory Management Parameters
1. Retain: Old value of release, new value of retain (applicable to OC object type), potential replication, only copying pointer.
2. Assign: direct value assignment (default, applicable to non-OC object types)
3. Copy: Old value of release, new value of copy, deep copy, content and pointer.

2> whether to generate the Set Method
1. readwrite: generate the declaration and implementation of setter and getter simultaneously (default)
2. readonly: Only getter declarations and implementations are generated.

3> multi-thread management
1. nonatomic: High Performance (this is generally used)
2. Atomic: low performance (default)

4> setter and getter Methods
1. setter: determines the name of the set method. There must be a colon:
2. getter: determines the name of the get method (generally used in the bool type)

With the help of the property parameter, our code saves a lot. Once we use the retain and assign parameters, the system will automatically convert our code to the standard code of the set and get methods, without manual writing. After adding the property parameter to the above Code, it becomes the following simplified code.

1 #import <Foundation/Foundation.h>2 #import "Car.h"3 4 @interface Person : NSObject5 6 @property (nonatomic,assign) int age;7 @property (nonatomic,retain) Car *car;8 9 @end
1 # import "person. H "2 3 @ implementation person 4 5-(void) dealloc 6 {7 [_ car release]; 8 nslog (@" % d Year Old Person object is recycled ", _ age); 9 [Super dealloc]; 10} 11 12 @ end
1 #import <Foundation/Foundation.h>2 3 @interface Car : NSObject4 5 @property (nonatomic,assign) int speed;6 7 @end
1 # import "car. H "2 3 @ implementation Car 4 5-(void) dealloc 6 {7 nslog (@" Car objects with a speed of % d are recycled ", _ speed ); 8 [Super dealloc]; 9} 10 11 @ end

 

Dark Horse programmer-memory management-Set Method Memory Management

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.