Search code examples
objective-cvariablesmemoryallocation

Objective c : Local variables do I need set it to nil after using it to release memory?


I am wondering, does local variable need to set to nil to deallocate the variable after use?


Solution

  • No. The memory is deallocated based on its retain count. Once that goes to zero it will be deallocated regardless of the value of the variable that used to point to it. Setting a variable to nil is a good practice so you don't try to access deallocated memory.