Why should we release?

后端 未结 2 681
花落未央
花落未央 2021-01-18 16:58

I often see when we release ab object we immediately set it to nil. I know that release and nil both free the old value associated with object but in case of release it leav

2条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-18 17:46

    Setting a pointer to nil does not release the memory occupied by the former destination of that pointer. In plain english, assigning it to nil does not release it.

    If your application is garbage collected, release is a no-op and can be left out. Otherwise, it's very, very necessary. Hence, Way 1 is always correct, and Way 2 is correct only under garbage collection.

    Note: This answer does not apply to projects using Automatic Reference Counting. Under ARC, setting a pointer to nil does send a release to the object.

提交回复
热议问题