_arc of objective-c Memory management

Source: Internet
Author: User

Memory management arc and auto-release Pool A variable ownership modifier in Arc

Variable modifiers, which are used primarily to identify the life cycle of an object. These concepts are not available in manual memory management methods.
There are several main variable ownership modifiers in the ARC environment:

__strong   __weak__unsa_unretained__autoreleasing

Detailed Description:
(1) Variable default value is __ Strong Decoration
An object cannot be freed as long as a strong reference exists. Objects are automatically destroyed when the scope of the object is exceeded and there is no strong reference.
The _strong property Basically adapts to all situations in the ARC environment. If you do not write, the default is the _ strong property.
(2) __weak does not hold objects, but simply refers to them.
That is,_weak does not affect the destruction of objects, as long as the __weak decorated object does not have a strong reference, it will be automatically destroyed, this time _ weak variable is automatically set to nil.
Here is an example

  NSString *__weak str = [][NSString alloc] initWithFormat:@"I am studying"];  NSLog (@"str : %@",str);

Because the Str object is a weak reference, the compiler will prompt, this is a weak reference, the result of the output: Str:null

When you open arc, you are not able to use the Retainrelease autorelease operation, the code that previously needed to manually add the reference count to handle memory management can be automatically completed by the compiler, but you need to use weak and strong on object properties, Where strong is equivalent to the Retain attribute, and weak is equivalent to assign, the underlying type or assign.

Second, automatic release of the pool
    • An auto-free pool is a collection of entities that can be objects that can be automatically freed.
    • (ID) autorelease;//is the method provided by NSObject, which, at a predetermined time, wants the object to send the release message, and the return value is the object that receives the message. In fact, when sending a autorelease message to an object, is to add this object to the auto-release pool (NSAutoreleasePool), and when the auto-free pool is destroyed, a release message is sent to all objects in that pool
Temporary objects and owning objects

When you use methods such as: Arraywithcapacity to get temporary objects, you do not have to consider memory release issues.
[NSColor Bluecolor]; A singleton object that is never destroyed, but you do not have to consider its memory problems.
If you rely on other objects in your custom class, you need to override the Dealloc method and release the dependent objects in this method
if you use more memory space in the design of the loop body, we recommend that you manually create an auto-free pool. such as:

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];   forint01000000; i++) {           id object = [someArray objectAtIndex:i];       NSString *desc = [object description];     // and do something with te descripton       if1000) {           [pool release];                            pool = [[NSAutoreleasePool alloc] init];       } }
The nature of APC
    • ARC is essentially a compiler that inserts the retain and release methods at the appropriate place in the compilation phase.
    • ARC is a compile-time feature.
About ROP and Non-rop

Attribution of Ownership

NSString *theString = @"Hello,Henan";   CFStringRef cfString = (CFStringRef)theString// __bridge     //指针的所有权不变,所以是 theString// __bridge_retained   cfString = (__bridge_retained CFStringRef)theString// 指针的所有权对象是 cfString//  __bridge_transfer  cfString = (__bridge_transfer CFStringRef)theString// 指针的所有权属于 theString
Some mandatory rules for using ARC
    1. You cannot call the Dealloc method directly, and you cannot call the Retain,release,autorelease,retaincount method, including the @ Selector (retain) method.
    2. If you need to manage resources instead of releasing instance variables, you should dealloc the method beforehand. You can no longer dealloc the method inside the [Super Dealloc] method, the dealloc of the parent class under Arc is also automatically completed by the compiler.
    3. Objects of the Core Foundation type can still be used to cfretain,cfrealese these methods.
    4. The Nsallocateobject and Nsdeallocateobject objects cannot be used.
    5. You cannot use object pointers in C structures, and if you have similar functionality, you can create a objective-c class to manage these objects
    6. There is no easy way to convert between ID and void *, and the same conversion between objective-c and Core Foundation types requires the use of a compiler-specified conversion function.
    7. The NSAutoreleasePool object can no longer be used, and ARC provides an @ autoreleasepool block to replace it, which is more efficient.
    8. Memory storage cannot be used (Nszone can no longer be used)
    9. You cannot give a property a name starting with new.
    10. Weak is generally used when declaring outlet, except for the top-level object in the middle of the nib StoryBoard such as strong.
    11. Weak equivalent to the old version of Assign,strong equivalent to retain.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

_arc of objective-c Memory management

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.