1.autorelease Basic Usage
- object is added to the auto-release pool when the Autorelease method is executed
- Automatically frees all objects in the pool for release when the auto-free pool is destroyed
- The Autorelease object does not change its own reference counter after executing the method, and it returns the object itself
Advantages of 2.autorelease
3.autorelease Use note
- Do not use when you are using a large memory object, fearing that the object will be released too late
- An object that uses less memory for operations can use
4.atureleasepool Auto-Release pool
Auto-free pool is stored in the in-memory stack followed by the "advanced out" principle
- #import <Foundation/Foundation.h>
- #import "Person.h"
- int main (int argc, const char * argv[])
- {
- Auto Free Pool 1
- @autoreleasepool {
- Release of object to auto-release pool to manage no more writing [person release]
- Person *person = [[[Person alloc] init] autorelease];
- Then create an auto-release Pool 2
- @autoreleasepool {
- Person *person2 = [[[Person alloc] init] autorelease];
- }
- Person *person3 = [[[Person alloc] init] autorelease];
- }
- return 0;
- }
As you can see from the code above, the first thing that executes the code is that the Person2 object is destroyed first, then the outer object, the person and the PERONS3 memory, behave as follows:
5. Using common errors
- Destroy the automatic release pool when you want to perform a release operation on the person, the field pointer error is reported
- @autoreleasepool {
- Person *person = [[[Person alloc] init] autorelease];
- [Person release];
- }
- Object executes two times autorelease means that the object executes two times when the auto-free pool is destroyed the release operation will report the wild pointer error
- @autoreleasepool {
- Person *person = [[[Person alloc] init] autorelease] autorelease];
- }
iOS basic memory management: Autorelease and Autoreleasepool