The difference between nil and release

Source: Internet
Author: User

Today and we explain in detail the difference between nil and release in Objective-c

First of all, the role of their two, nil is to put an object's pointer to null, that is, we say the pointer to zero, just cut off the pointer and in-memory object, it does not play a role in memory release, and release is really trigger memory release, After release, the Block memory is marked as usable (redistributed), and when Alloc and new objects are used, release releases the memory when the current class is destroyed and calls Dealloc. So nil does not release the memory, only release is the real departure to 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 between nil and release

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.