Objective-C 2.0 Study Notes (5)

Source: Internet
Author: User
Tags finally block

The following are the notes for learning objective-C 2.0. The original book I purchased is the original English version. Due to my limited level of English, there may be omissions in understanding.

Original book purchase address: Amazon


5. Memory Management


Item 29: Understand reference counting

Some knowledge points:

(1) The returned parameter is autorelease. If you need to assign a value to the instance variable, You need to retain it and release it in a proper place.

Remember:

(1) The reference counter memory management method is based on an ever-increasing and decreasing counter. When an object is created, the counter is 1, the counter is positive, and the counter is released when the counter is 0.

(2) The reference counter of an object is continuously increased and reduced by different objects throughout its lifecycle. Different objects control their respective addition and reduction operations.


Item 30: Use arc to make reference counting easier

Some knowledge points:

(1) The retain, release, autorelease, and dealloc methods cannot be called in Arc mode.

(2) Objects returned by methods prefixed with alloc, new, copy, and mutablecopy belong to the caller.

Remember:

(1) arc frees developers from complicated memory management. Using Arc can greatly reduce some sample code of the class.

(2) add necessary retention and release operations to the arc wherever appropriate throughout the lifecycle of the object. A large number of valid attributes are added to manage memory operations.

(3) The method name is also used for the memory management reference of the returned object. Arc retains some naming keywords, which developers should use with caution.

(4) arc is only used for objective-C objects. This means that some underlying objects are not applicable. For example, the cfretain/cfrelease method is still necessary.


Item 31: Release references and clean up observation State only in dealloc

Remember:

(1) The dealloc method is only used to release the reference of other objects and cancel some registration notifications, such as KVO and nsicationcenter center.

(2) If an object holds a system resource, such as a file system, you need to define a method to release the resource. In addition, it should be agreed that the developer should release the service after the call is complete.

(3) Some methods that attempt to execute asynchronous operations or call methods that assume the object is in the normal state cannot be called in the dealloc method, because the state of the object cannot be guaranteed when the dealloc method is executed.


Item 32: Beware of memory management with exception-safe code

Some knowledge points:

(1) in non-arc mode, the memory space applied in the try block can be released in the Finally block.

Remember:

(1) When an exception is obtained, make sure that all the memory space applied in the try block has been released.

(2) by default, the arc mode does not add effective memory cleanup code for exceptions. This problem can be solved by adding the compilation option, but the generated code will be larger and cause more runtime consumption.

Item 33: Use Weak references to avoid retain cycles

Some knowledge points:

(1) unsafe_unretained still points to the released object after the object is released; weak automatically points to nil after the object is released.

(2) unsafe_unretained is semantically equal to assign, but the former is used for pointer objects, and the latter is used for simple data types.

Remember:

(1) The weak attribute can be used to break a circular reference.

(2) objects in the weak attribute may not be empty automatically. The automatic null setting feature is implemented by arc at runtime. Automatically empty weak reference can always be safely read, because it will never point to unallocated memory objects.

Item 34: Use autorelease pool blocks to reduce high-memory waterline

Remember:

(1) the automatic release pool is a running rule in the stack form. Each object sent to the automatic release pool is added to the top of the stack.

(2) The current application uses the automatic release pool to effectively reduce memory consumption.

(3) The @ autoreleasepool syntax is more effective for the latest Auto Release pool.

Item 35: use zombies to help debug memory-management problems

Some knowledge points:

(1) The Zombies attribute puts all released objects into a zombie object instead of clearing them directly.

Remember:

(1) Use the nszombieenabled flag to convert a released object to a zombie object instead of clearing it directly.

(2) An ISA pointer to a botnet object points to a botnet class. All methods taken by this zombie object will cause the program to crash and provide a message.

Item 36: Avoid using retaincount

Remember:

(1) The reference count value of an object seems useful, but it is not very useful, because reference count values at any time cannot reflect the full lifecycle of the object.

(2) When the arc arrives, the retaincount method has been discarded and calling this method will cause compilation errors.

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.