"Objective-c Study record" 28th Day

Source: Internet
Author: User
Tags shallow copy



iOS memory management is a reference counting mechanism that is divided into MRC (Manual reference counting) and arc (auto reference count).



The idea of managing memory with reference counts is to implement operations that generate, hold, release, and destroy objects by controlling memory or object references.



If the number of increases is greater than the number of reductions, it will cause memory leaks;



If the number of reductions is greater than the number of increases, it can cause excessive release;



If the number of increases is equal to the number of reductions, also continued access, will result in wild pointers.



1. Build: Object reference count from 0 to 1



2. Hold: Add a reference to the object's reference count plus 1



3. Release: Reduce a reference to the object's reference count plus 1



4. Destroy: When the object's reference count to 0 o'clock (in fact not 0, explained later), the system will reclaim this memory space



When the space is reclaimed by the system, it is not possible to access the space through a pointer, which can easily cause a wild pointer.



Attention! The concept of reference counting exists only in the heap area, against the heap area of the object.



Retaincount method



You can get the value of the reference count of the object by changing the method, and the return value is Nsuinteger.



1. Build



+ (instancetype) alloc; Creating a memory space in the heap area, releasing the object, and zeroing out the memory, while changing the reference count of this object to 1, is the process from 0 to 1.


1 Hero *hero = [[Hero alloc] init]; // This process opens up memory space for the Hero object, and the reference count adds 1


It is important to note that the Direct assignment reference count does not increase , as follows:


1 Hero *am = Hero// does not cause Hero object reference count plus 1


2. Holding



retain; Adds 1 to the object's reference count.


1 [hero retain]; // at this point the reference count for hero is 2 2 [am retain]; // at this point the reference count for Hero/am is 3


3. Release



a.release; Reduce the object's reference count by 1, and subtract 1 immediately.


1 [hero release]; // the reference count for hero is 2 2 [hero release]; // the reference count for hero is 1 3 [hero release]; // the reference count for hero is 1


It is important to note that after three successive releases of the Hero object, we can view the value of the reference count of the hero in Xcode through the Retaincount method, and the result is not the 0 we expect, because the system internal reference count value does not have 0. 0 is only a number of people and people directly convenient communication and understanding, but in essence, the object has been released, if it is possible to access the Hero object can cause a wild pointer (the reclaimed memory space is occupied by other pointers or variables, again through the original pointer to access the memory).



That is, the reference count value is a minimum of 1, not 0.



b.autorelease; This method also lets the object's reference count minus 1, but unlike release, it does not immediately subtract 1, but at some point in the future, it triggers a minus 1 operation. This action is related to the auto-release pool.



Auto-Release Pool (autoreleasepool)



The auto-free pool is a container that records the Autorelease messages received by the inner objects of the pool, which objects receive, received several times, who receives them first, who receives them, and when the pool is released, it will reduce the 1 operations based on the information of these records.



It is important to note that the auto-release pool is similar to the stack area, followed by the advanced out-of-space first, after the reduction of 1 operations.


1 Hero * sf = [[Hero alloc] init];
2 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
3 [hero autorelease];
4 [am autorelease];
5 [sf autorelease];
6 [pool release]; // When this statement is executed, the pointer marked above is decremented by 1; usually, the system will automatically complete this function 


4. Destruction



Dealloc; Destroys an object from memory. This method can be overridden directly in the. m file of the class and is automatically called when the object is actually destroyed from memory:


 
 
1 - (void)dealloc
2 {
3     NSLog("%@ is dealloc", self);
4     [super dealloc];
5 }


You can compare the destruction method with the initialization method:



Initialize: Destruction:



[Super Init] Releasing instance variables



Initializing instance variables [Super Dealloc]



It is not difficult to find that the destruction method is the reverse of the initialization method. So as long as you rewrite the Dealloc method, [Super Dealloc] is always on the last line. It is divided into two steps: 1. Release the instance variable of itself 2. Executes the Dealloc method in the parent class, releasing the inherited instance variable.



Agreement



Similar to interface objects in Java C + +



Copy, requires class to use Nscopying protocol



1. Pseudo-Copy



Features: Equivalent to no copy, just let the outside of the object to perform a retain operation.


 
 
1 - (id)copyWithZone:(nullable NSZone *)zone
2 {
3     return [self retain];
4 }    


2. Shallow copy



Features: The copy is the address, initializes a new object, but the new object and the old object share a copy of the content, changing the value of one of the object instance variables, the other will also access to the value after the change.



Attention! If the instance variable is a string and it points to a constant area when initialized, changing the value of the string after copying is equivalent to re-opening a new space in the constant area, so this situation does not affect the value of another object instance variable.


 
1-(id) copyWithZone: (NSZone *) zone
2 {
3 // instantiate a new object
4 Hero * hero = [[Hero allocWithZone: zone] init];
5 // Assign a value to the instance variable of the new object
6 Hero.name = self.name;
7 Hero.level = self.level;
8 return hero;
9 }


3. Deep copy



Feature: An object is reinitialized, and the memory is two copies, changing one of the values, and the other does not change. Can be understood as copy-and-paste operations.


 
1 - (id)copyWithZone:(NSZone *)zone
2 {
3      Hero *hero = [[Hero allocWithZone:zone] init];
4      hero.name = self.name;
5      hero.level = self.level;
6      return person;
7 }


"Objective-c Study record" 28th Day


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.