The difference and usage of nil and release in Objective-c

Source: Internet
Author: User

First of all, the role of their two, nil is to put an object's pointer to empty, just cut off the pointer and in-memory objects, it does not have any effect on the release of memory, and release is really used for memory release, after release, the system will mark the Block memory is available (can be reassigned). So nil does not release memory, only release is really free memory.

The two use order, if there is no release on the direct nil, then although there is no error (release a null pointer is legal), but it is tantamount to creating a memory leak, because nil after the release has no effect, My previous lesson was to put nil before release, so leak has been reporting memory leaks.

Conversely, if you set nil after release, there will be no such problem, but someone will ask, release without setting nil, what will happen? In fact, the program may not error, but to know that the set nil is actually to prevent pointer confusion, because an object in the release, the allocated memory has been released, if you do not empty the pointer after release, the system again misused to this pointer, Then the program crashes (this is especially easy to do in the delay call function), and if it is released and the pointer is empty, it will not crash even if the subsequent program uses the pointer. So objective-c must release and nil when releasing memory.

The difference and usage of nil and release in Objective-c

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.