iPhone - dealloc - Release vs. nil

前端 未结 5 1955
情书的邮戳
情书的邮戳 2020-11-28 21:18

Wondering if someone with experience could possibly explain this a bit more. I have seen examples of...

  [view release];

  view = nil;  

5条回答
  •  执笔经年
    2020-11-28 22:02

    What you have seen is probably these:

    1) [foo release];
    2) self.bar = nil;
    3) baz = nil;
    
    1. Is releasing the object, accessing it through the instance variable foo. The instance variable will become a dangling pointer. This is the preferred method in dealloc.

    2. Is assigning nil to a property bar on self, that will in practice release whatever the property is currently retaining. Do this if you have a custom setter for the property, that is supposed to cleanup more than just the instance variable backing the property.

    3. Will overwrite the pointer baz referencing the object with nil, but not release the object. The result is a memory leak. Never do this.

提交回复
热议问题