When the pointer count of a small test object is set to nil

Source: Internet
Author: User

This article reposted on the new wind wave BLOG column, blog address: http://blog.csdn.net/duxinfeng2010/article/details/8757211

Original post of the following blog:

 

I encountered such a problem recently, but I didn't pay attention to it before. I created a project and added a class. The file structure is like this.

Then write such a short program to run

- (*testObj =

 

Obviously, the retainCount count should be 0, but why is the retainCount count still 1? When we perform the release operation twice, the program crashes;

 

Then we can print the testObj object.

 

- (*testObj =

 

They point to the same address;

When testObj is in alloc, a space is applied to the heap, and its retainCount is counted as 1. After the release, the space indicated by testObj is destroyed and does not exist. In this case, testObj is a wild pointer. Calling [testObj retainCount] is an insecure method;

 

Then we add testObj = nil after adding a line of code [testObj release;

- (*testObj ==

Alas, you should understand it here. After [testObj release], testObj still points to, but the space originally pointed to by testObj has been destroyed, but it still exists, by setting this step to nil, we will not point to the original address. We should remember the role of the viewDidUnload function. It is doing this job. When we declare an object attribute, set this attribute to nil in viewDidUnload;

 

Note: After ios6.0, The viewDidUnload method has been deprecated, so where to set the attribute to nil? Wow, I am very confused about whether to release and set the attribute to nil in dealloc. I don't know how netizens do it.

 

My personal summary: It seems that the original object is released, but when it is not set to nil, the space requested by the original object is still 1 because the Pointer Points to the count, it is released only after being set to nil. 0 .. 0

I don't know if this is the case ..

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.