Objective-c Knowledge Summary (3)

Source: Internet
Author: User

Manual memory Management MRC

First introduce the reference counter : To save the current object there are several things to use it (number)

the role of the reference counter : To determine whether the object should reclaim memory space (if the object is not equal to nil, when the reference counter is 0, the object's memory space is reclaimed)

To reference the counter operation :

    • Retain make reference counter +1

    • Release-enabled reference counter-1

    • Retaincount get the value of the reference counter

If an object is freed, the object's Dealloc method is called

Attention:

    • Dealloc method is NSObject, generally we want to rewrite the Dealloc method
    • Within the Dealloc method, the [super Dealloc] is called;

Scope of memory management:

    • Memory management of all nsobject-integrated objects

    • Data memory for the base data type (int double float char struct enum) does not require us to manage

Principles of memory Management:

    1. If the object is being used, it should not be recycled

      If you want to use this object, you should let this object retain once

      If you don't want to use this object, you should let this object relase once.

    2. Who created who release

    3. Who retain who release?

The contents of memory management research:

    1. Wild hands:

      1) The defined pointer variable is not initialized 2) The space pointed to has been freed

    2. Memory leaks:

      {             *p  new];              } /*              P Stack area             [person new];  Heap Area              If P has been released, and the heap space has not been released, the space in the heap has been leaked * /

Set Method Memory Management

//   for an instance variable of an object as another class -(void) Setdog: (dog*) Dog {       //   Determines whether an object is the original object        if (_dog! = dog)            {///2) Release old value            [_dog release             ]; // retain the new value, and assigns the value to the instance variable           _dog = [dog retain];        }    }

Cyclic retain problems

Cyclic retain causes two objects to be memory-compromised

Prevention methods:

    1. Let an object be released more than once (note the order)

    2. One end using retain (recommended) using the Assign end

Memory management issues for the NSString class

#import<Foundation/Foundation.h>intMainintargcConst Char*argv[]) {@autoreleasepool {//Defining Strings//a constant pool of strings,//If you need a string that already exists in the constant pool, no memory space is allocated//when using a string,//@ "abc" stringwithstring alloc initwithstring are in constant area///0x100001030 SmallNSString *STR1 =@"ABC";//constant at @ "abc" stringNSString *STR3 = [NSString stringwithstring:@"ABC"];//Constant AreaNSString *STR5 = [[NSString alloc] initwithstring:@"ABC"];//also in the constant areaNSString *STR6 = [[NSString alloc] init];//Constant AreaSTR6 =@"ABC"; //0x100202030 Big//if the str2 STR4 address in the constant area should be the same//actually not the same so str2 STR4 are in the heap areaNSString *STR2 = [NSString stringWithFormat:@"ABC"];//not in the stack area, in the heap areaNSString *STR4 = [[NSString alloc] Initwithformat:@"ABC"];//not in the stack area, in the heap area//0x7fff5fbff764        intA =Ten;//Stack AreaNSLog (@"str1 =%@,%p,%lu", Str1,str1,str1.retaincount); NSLog (@"str2 =%@,%p,%lu", Str2,str2,str2.retaincount); NSLog (@"STR3 =%@,%p,%lu", Str3,str3,str3.retaincount); NSLog (@"STR4 =%@,%p,%lu", Str4,str4,str4.retaincount); NSLog (@"STR5 =%@,%p,%lu", Str5,str5,str5.retaincount); NSLog (@"STR6 =%@,%p,%lu", Str6,str6,str6.retaincount); NSLog (@"A =%p",&a); }    return 0;}

Auto-Release pool: Special stack structure

Characteristics:

    • Object can be added to the auto-release pool
    • When the auto-free pool ends, a release message is sent to the objects in the pool

Use of the auto-free pool:

    1. Create an auto-free pool
      @autoreleasepool {      }

2. Join the auto-release pool

/*   in the auto-release pool  [object  autorelease]; */

Simulates a person class class with an object method-(void) run;

#import<Foundation/Foundation.h>#import "Person.h"intMainintargcConst Char*argv[]) {    //1 Creating an auto-free poolPerson *p = [personNew];//P 1@autoreleasepool {//Auto-release pool start[P run]; NSLog (@"%lu", P.retaincount);//1//[P autorelease] Add the object p to the auto-release pool//Note: The reference count does not change after you join the auto-release pool[P autorelease];//Join the auto-release pool,NSLog (@"%lu", P.retaincount);//1[P run]; }//Auto Free pool end [P release];[P run]; return 0;}

Objective-c Knowledge Summary (3)

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.