Black Horse programmer _ objective-C Memory Management notes

Source: Internet
Author: User
Reference Counter

 

When an object is created, it must be allocated to the memory object. When this object is not used, it must be recycled in a timely manner. In order to clearly know whether the object has been used, it must be reflected by the reference counter. As long as the counter is not 0, it indicates that the object is in use.

 

1. Basic use of methods

1> retain: Counter + 1, will return the object itself

2> release: Counter-1, no return value

3> retaincount: obtains the current counter.

4> dealloc

* When an object is to be recycled, it will call

* Be sure to call [Super dealloc]. This call should be placed at the end.

 

2. Concepts

1> zombie objects: the occupied memory has been recycled. Zombie objects cannot be used any more.

2> wild pointer: pointer to a zombie object (memory unavailable). An error is returned when a message is sent to the wild pointer (exc_bad_access)

3> NULL pointer: no pointer pointing to anything (nil, null, and 0 are stored). No error is returned when messages are sent to null pointers.

 

Common Errors:

 

Exc_bad_access: access to a bad memory (recycled and unavailable)

 

-[Person setage:]: Message sent to deallocated instance 0x10020.a10

A-setage: message is sent to the released object.

 

Set Method Memory Management

 

If a pointer points to a new object, the counter of the object will add 1. When the pointer points to a new object, the memory occupied by the original object should be released in time. At this time, the set method needs to be rewritten to manage the counter of the object.

-(Void) setcar :( car *) Car {If (car! = _ Car) {// perform a release [_ car release] on the current car (old car); // perform a retain operation on the new car _ car = [Car retain];}

 

 

Note: memory management is not required for basic data types.

 

The dealloc method is automatically called when the object is recycled.

 

 

- (void)dealloc {    [_car release];    [super dealloc]; }

 

 

Memory Management Code specification:

As long as alloc and copy are called, there must be release (autorelease)

Objects are not generated through alloc or copy, so release is not required.

 

 

Autorelease and autoreleasepool

 

 

1. Basic usage of autorelease

1> objects are placed in an automatic release pool.

2> when the automatic release pool is destroyed, all objects in the pool are release once.

3> returns the object itself.

4> after the autorelease method is called, the counter of the object remains unchanged.

 

2. Advantages of autorelease

1> no longer care about the time when the object is released

2> no need to worry about when to call release

 

3. Notes for using autorelease

1> do not use autorelease for objects that occupy a large amount of memory.

2> the use of autorelease for objects with small memory usage has no significant impact.

 

 

4. incorrect syntax

1> after alloc, autorelease is called and release is called.

 @autoreleasepool {    // 1    Person *p = [[[Person alloc] init] autorelease];     // 0    [p release]; }

 

 

2> multiple consecutive autorelease calls

 

@autoreleasepool {    Person *p = [[[[Person alloc] init] autorelease] autorelease]; }

 

 

5. automatically release the pool

1> when the IOS program is running, countless pools are created. These pools all exist in the stack structure (Advanced and later)

2> when an object calls the autorelease method, it is placed in the release pool at the top of the stack.

 

Note:

1. The autorelease method returns the object itself.

2. After the autorelease method is called, the counter of the object remains unchanged.

3. autorelease puts the object in an automatic release pool.

4. When the automatic release pool is destroyed, all objects in the pool are release once.

Black Horse programmer _ objective-C Memory Management notes

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.