iphoneiosios4deallocmemory-management

Why should I do [object release]; object=nil; when deallocating an object?


I would like to understand why it could be useful to do this (assuming "object" was previously allocated):

[object release]; 
object=nil;

Thx for helping,

Stephane


Solution

  • Even though you release an object, your variable will still point to something. What it points to is undefined. It could still point to the old object, or to some point in memory. Setting it to nil avoids sending messages to whatever it points to, and prevents errors (messaging nil does nothing).

    Here's an answer that states it better: Setting pointers to nil, objective-c