OBJECTIVE-C (7) memory management MRC

Source: Internet
Author: User

    • OBJECTIVE-C Memory Management

Management Category: OC objects

The memory resources that each computer system can use for the program are limited. There are two things we should care about:

Allocate when needed, release after use

Do not use any memory resources that have been freed, otherwise useless stale values cause a variety of errors to occur

Three kinds of management methods:

Manual admin Mode MRC (Manual referencing count)

Semi-automatic management mode Autoreleasepool

Automatic management mode ARC (auto referencing count)


    • Reference counting and memory management guidelines

Cocoa employs a technique called reference counting, which associates an integer for each object:

Related methods:

-(Instancetype) retain//reference count +1-(void) Release//reference count-(Nsuinteger) Retaincount//object's current reference meter Numerical

Use reference count:

A. When an object is created (alloc new copy), the reference count of the object being created is 1

B. The reference count is +1 (retain) when a piece of code accesses the object

C. When this code is complete, the reference count-1 (release)

D. When the reference count is 0 o'clock, indicating that no code is accessing the object, the object is destroyed

(An dealloc message is automatically sent to it before it is destroyed).


Scene interpretation: In-game rooms, replicas, etc.


Guidelines for Memory management:

1) Principles of memory management:

As long as someone else is using an object, the object will not be recycled;

As long as you want to use this object, then you should let the object reference counter +1;

When you do not want to use this object, you should let the object reference counter-1;

2) who created, who release

A. If you create an object through Alloc,new,copy, you must call the release or Autorelease method

B. If you don't create it, you don't have to be responsible.

3) who retain, who release

Summary: The beginning and ends, there are plus and minus.


    • Code specifications in the MRC

1) As long as the alloc is called, there must be a release

2) The setter method of the attribute

A. Direct assignment of basic data types

-(void) Setage: (int) Age {_age = age;}


B. OC object, first judge and attribute old value is not the same object

If it is, do nothing; if not, release the old value and retain the new value

-(void) Setcar: (Car *) Car {if (car! = _car) {[_car release];    _car = [car retain]; }}


3) Dealloc method

A. Release once for properties held by self

b. [Super Dealloc] put in the last

-(void) Dealloc {[Car release]; [Super Dealloc];}


4) do not show the following junk code

Stu.car = [[Car alloc] init];  Causes the reference count to be 2[[car alloc] init].speed = 100; I can't release it.


    • @property Memory Management parameters

@property should have only one memory management parameter.

Under MRC, memory management parameters include: Assign retain copy, default is assign

These memory management parameters can determine the implementation of the property setter method

Assign:setter does not modify the reference count

@property (nonatomic,assign) Nsinteger age;

Setter Method:

-(void) Setage: (Nsinteger) Age {_age = age;}

Retain: Modify the reference count for this property (as per the Code specification)

@property (nonatomic, retain) nsnumber * age;

Setter Method:

-(void) Setage: (NSNumber *) Age {if (_age! = age) {[_age release];    _age = [age retain]; }}


Copy: Deep copy assignment, generally used for nsstring

@property (nonatomic, copy) NSString * name;

Setter Method:

-(void) SetName: (NSString *) name {if (_name! = nil) {[_name release]; } _name = [name copy];}

Circular reference problems with object composite relationships

Problem Description:

A person object, a card object, each property

If one memory parameter is retain, then the reference count should be 2

Do this for any object release, no object is destroyed

Workaround:

One end uses retain and one end uses assign



This article is from the "Teacheran" blog, make sure to keep this source http://annmeng.blog.51cto.com/3321237/1745623

OBJECTIVE-C (7) memory management MRC

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.