Note that there are some explanatory texts on larger screens.

plurals
  1. POiOS Memory Management followup. dealloc vs nil?
    primarykey
    data
    text
    <p>With regards to this posting: <a href="https://stackoverflow.com/questions/1458178/iphone-dealloc-release-vs-nil">iPhone - dealloc - Release vs. nil</a></p> <pre><code>1) [foo release]; 2) self.bar = nil; </code></pre> <p>The explanation was as follows:</p> <ol> <li><p>Is releasing the object, accessing it through the instance variable <em>bar</em>. The instance variable will become a dangling pointer. This is the preferred method in dealloc.</p></li> <li><p>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.</p></li> </ol> <p>Could someone please clarify the explanation for #1? Accessed through the instance variable <em>bar</em> ?</p> <p>E.g I have set a private var in my object header as so:</p> <pre><code>SomeObject *oPointer; </code></pre> <p>I am not using a property setter with this pointer in the header file and it does <strong>NOT</strong> get synthesized when instantiate the object.</p> <p>In my code, given certain conditions, I later have to allocate and assign this pointer to it's object.</p> <pre><code>obj = [[SomeObject alloc] initWith....]; </code></pre> <p>So this is now accessed through the instance variable <em>obj</em>. I have a UIButton which is configured to RESET this object it's attached method deallocs it. I do this via:</p> <pre><code>[obj release]; obj = nil; </code></pre> <p>After all that explaining the question is, why do I also <strong>have</strong> to declare the obj = nil? The <em>[obj release]</em> call seems to also kill the pointer. I was thinking that <em>[obj release]</em> would deallocate the memory it's pointing to and also set <em>obj</em> to nil all in one shot but it seems it also kills the pointer because my app crashes when it tries to reference <em>obj</em> after <em>[obj release]</em>;</p> <p>Does this question make sense? Is the explanation simply that <em>[obj release]</em> does <em>ALL</em> cleanup, including killing the pointer and I need to be aware of that? </p> <p>If I set a retain property for the SomeObject pointer would the pointer still be retained after release? </p> <p>Thanks in advance!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload