1

I read about manual & ARC memory management in Objective-C. In below points I am confused which is true about memory management in Objective-C?

  • Always nil out properties in dealloc under ARC and manual memory management.

  • Do not have to nil out properties in dealloc under ARC and manual memory management.

  • nil out properties in dealloc under ARC but not in manual memory management.

  • nil out properties in dealloc under manual memory management but not in ARC.
bneely
  • 9,083
  • 4
  • 38
  • 46
TechChain
  • 8,404
  • 29
  • 103
  • 228

3 Answers3

1

You never have to "nil" out properties, you just have to release them when doing manual memory management.

When you set a value to nil, you lose track of the old value that was there. That old value stored the address of the object. Now that object continues to exist, but you no longer know its address, and have no way of releasing it.

Release tells an object "I'm no longer using you, and if I'm the last one, then delete yourself". In ARC, retain and release calls are added automatically by the compiler. There's rarely any reason to do that manual memory management yourself anymore. ARC is the way to go.

Alexander
  • 59,041
  • 12
  • 98
  • 151
  • When you set a value to nil, ARC will release the object. – Willeke May 17 '16 at 01:36
  • He's specifically talking about `dealloc`. Suppose I have a variable `foo` that points to an object. Setting it to `nil` would cause the compiler to add in a `release` call. But this is happening in the `dealloc` method, the object that `foo` is stored in is itself about to be destroyed. That would decrease the reference count. No need to `nil` it. – Alexander May 17 '16 at 13:38
  • As my answer states: "You never have to "nil" out properties," – Alexander May 18 '16 at 17:35
  • Obviously, the categorical "you never have to 'nil' out properties" is far too broad. This statement applies narrowly to an object's own properties within the context of its `dealloc` method. But in `dealloc`, you frequently `nil` any `weak` or `unsafe_unretained` references to the object being deallocated (e.g. common for delegates, notifications, KVO, etc.). Likewise, outside the context of `dealloc`, you `nil` properties all the time. But your point is well taken, that within `dealloc`, you don't have to `nil` any of the object's own properties (but in MRC you do have to `release` them). – Rob May 27 '16 at 17:01
  • 1
    Yes, my statement was to be taken in the context of the question. – Alexander May 27 '16 at 17:28
1

There isn't much reason to use manual memory management in most cases anymore.

But to answer your question, don't nil in dealloc. Instead:

  1. Do nothing with properties in dealloc with ARC.
  2. release strongly held properties in dealloc in manual management. [myObject release];
Dancreek
  • 9,524
  • 1
  • 31
  • 34
0

Just use ARC and happy days!

However, you should at least be aware of the difference between strong and weak references.

Community
  • 1
  • 1
Brett Donald
  • 6,745
  • 4
  • 23
  • 51