iOS memory management mechanism analysis of MRC manual reference counting mechanism

Source: Internet
Author: User

Objective:

iOS memory management mechanism arc and MRC are the basic must-ask questions for programmers to attend an interview, and also an iOS basic
No solid key, so in-depth understanding of the importance of memory management mechanism is self-evident.

History of iOS memory management mechanism
    • Before IOS 5: MRC (Manual reference count)

    • IOS 5 and later: ARC (auto-ingest count)

The era of MRC mechanism

"Who opened the application, who promptly and reasonably released" Face the memory space of their application is to be recycled in a timely manner:

What happens if you don't release it in time?

Objects are stored on the stack and may be heavily memory-intensive, causing the program to Flash (that is, memory leaks)

What are the consequences of unreasonable release?

Release prematurely, and if you continue to reference the object later, a crash occurs, with the EXC_BAD_ACCESS crash prompt for an object that has already been disposed of. (That is, the wild pointer)

The MRC era is a bit of a pain for iOS programmers, so let's go into a bit of an analysis

Principle of memory management--pairing principle

Explanation: Make reference count (Retaincount) +1 must correspond to the occurrence of the reference count-1

How do I make a reference count of +1?

newcopy(mutablecopy) retain alloc

How to make reference count-1?

releaseautorelease

For example:
[对象 release];  reatinCount-1[对象 retain];   reatinCount+1,并且返回self
Determine if an object can be reclaimed by the system?

The only object that can be reclaimed by the system is the reference count of the object (Retaincount) is 0

Talk about the Dealloc function again.
     //当实例变量的引用计数为0,系统会自动调用dealloc函数进行摧毁回收     - (void)dealloc{}  
When overriding the Dealloc function, it should be noted that:
    1. To override Dealloc, you need to use the Dealloc function of the parent class

    2. If there are subclasses that need to be destroyed, the order should be above the parent class to avoid unnecessary errors.

      - (void)dealloc {          [子类变量  release];          [super dealloc];  }   
How ARC Engineering turns into MRC engineering

Yes-->no

Common error Preparation in MRC combat

Create a project –> create a class name named List---class to create a name variable-- @property NSString *name; and override the Dealloc method (so that memory is used properly if not directly according to the printed view)

    -(void)dealloc    {       NSLog(@"我的内存要被释放了");       [super dealloc];    }
1. The use of variables does not follow the pairing principle, resulting in a memory leak
#import <Foundation/Foundation.h>#import "List.h"int main(int argc, const char * argv[]) {@autoreleasepool {    List *l=[[List alloc]init];   // reatinCount=1    [email protected]"苹果";    NSLog(@"%lu",l.retainCount);    //[l release];      }      return 0;  }

The reference count for L at the end of the run is still 1, and there is no call to Dealloc

2. Follow the pairing principle but the memory leak caused by error using nil
     List *l=[[List alloc]init];     [email protected]"苹果";    NSLog(@"%lu",l.retainCount);    l=nil;    [l release];

When manipulating the class object, I like to say the object =nil after the last release, to avoid the crash of referencing the object elsewhere, but the location is incorrect, the memory leaks like the one above [L release] are equivalent to [nil release], and the reference count of L is 1. And not released in time.

3. Early release of the object, causing the wild pointer operation
 List *l=[[List alloc]init];    [email protected]"苹果";    NSLog(@"%lu",l.retainCount);    List *l1=l;    [l release];    NSLog(@"%@",l1.name);  

L1=l, there are two pointers pointing to the same class address at this point, when a pointer release causes Reatincount=0 to release the memory space of the class, and the other pointer will also point to null, which throws a wild pointer exception

4. When an object Retaincount is already 0 o'clock, calling the Retain method will not bring the object back to the dead, while the wild pointer operation exception will occur.
    List *l=[[List alloc]init];    [email protected]"苹果";    NSLog(@"%lu",l.retainCount);    [l release];    [l retain];
5.MRC override of Set and get methods
@interface List : NSObject{    
@implementation List-(void)setName:(NSString *)name{    if (_name!=name) {    [_name release];    _name=[name retain];    }}-(NSString *)name{    return _name;}@end

iOS memory management mechanism analysis of MRC manual reference counting mechanism

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.