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
- You cannot call the Dealloc method directly, and you cannot call the Retain,release,autorelease,retaincount method, including the @ Selector (retain) method.
- 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.
- Objects of the Core Foundation type can still be used to cfretain,cfrealese these methods.
- The Nsallocateobject and Nsdeallocateobject objects cannot be used.
- 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
- 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.
- The NSAutoreleasePool object can no longer be used, and ARC provides an @ autoreleasepool block to replace it, which is more efficient.
- Memory storage cannot be used (Nszone can no longer be used)
- You cannot give a property a name starting with new.
- Weak is generally used when declaring outlet, except for the top-level object in the middle of the nib StoryBoard such as strong.
- 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