Reference count for memory management problems in OC and oc Memory Management

Source: Internet
Author: User

Reference count for memory management problems in OC and oc Memory Management

Define a Person class

In fact, the reference count problem can be understood as a house rental problem. If retain alloc is used to represent the person who rented the big house and obtained a key, the reference count indicates the total number of keys in the house, release indicates the return key for check-out

Person * p = [[Person alloc] init]; // The landlord has built a house and is now a landlord.

// Open up the space in alloc. The reference count is changed from 0 to 1.

// RetainCount, used to view the reference count of an object

NSLog (@ "% lu", [p retainCount]);

Person * p1 = [p retain]; // retain once, representing another Person renting a house

NSLog (@ "p1 = % lu", [p1 retainCount]); // reference count 1-2

NSLog (@ "% lu", [p retainCount]); // reference count 1-2

Person * p2 = p1; // No retain or alloc reference count unchanged. It is equivalent to p1 to bring his friends, friends do not have a key

// Release reduce reference count (Return key)

[P release]; // reference count 2-1

[P release]; // The reference count is 1-0 (but if we output it, it may be a crash or 1, for the following reasons :)

NSLog (@ "% lu", [p retainCount]); // It must be wrong to write this statement first. In the last release, the system has recycled this memory and the space has been returned to the system, the space is no longer managed by you. If you access the space at this time, you will access a space without ownership. Therefore, the following operations may cause a wild pointer problem.

We have also taken the above house rental problem as an example. Now the last person in the house has left, and no one has lived. The keys have been handed in, but our current operation is like opening the door with his key, which is very insecure. why does it sometimes crash and sometimes print out 1? This is because if the memory is not used by others, it is like the house has not been rented out, it is safe for you to go back and get the key to open the door, but if the House has been rented out, that is, other data is using the memory, then you can go back and open the door, so your situation is quite insecure ???

[P release]; // native pointer problem + transition release. This is also easy to understand. You can understand that a tenant should not only open the door, but also hold the key and return it again, I want the landlord to refund the house deposit again. It is illegal and the House crashes ~


In fact, in addition to the wild pointer and excessive release mentioned above, there is also a memory leak, which we can understand as a memory leak. One is that the last key for renting a house is not returned, in this way, the landlord could not open the house, and the house could not be rented out again, resulting in a waste of memory. Of course, when the landlord rented another house, it had no big impact on him, however, once there are a lot of tenants and the house is not enough to rent, the landlord will get slower and slower to earn money, which may lead to the inability to continue ~

This article is just a matter of my understanding about memory reference count ~

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.