-1

As I understand in ARC, you don't need to release objects manually, it's being done for you automatically. But how much responsibility is still mine? I know that local variables are being set to nil automatically at the end of the method. But what about strong properties? Do I have to set them to nil in methods like dealloc or viewDidUnload? Are strong properties released automatically? And what about instance variables, are they same as properties? I understand that not all the responsibility for memory management is taken from me.

Kate Gregory
  • 18,808
  • 8
  • 56
  • 85
Ani
  • 33
  • 3

2 Answers2

0

Strong properties include a reference count that got increased when your object made the reference to them, and when you set them to nil (either explicitly, or by having your object ready to be cleaned up automatically), their reference count is decreased. If at that time their reference count is zero, they can be deallocated as well.

Things to be cautious of are circular references, such as object A having a strong reference to object B, while object B has a strong reference to object A. This is a problem for automatic clean up of these objects.

mah
  • 39,056
  • 9
  • 76
  • 93
0

Basically the compiler adds retains and releases for you. It also releases strong references when the class deallocs. You only need to write a dealloc method if there is some code that needs to run at that time and you do not need to, infact can not, call [super dealloc], that is done for you.

But:

I know that local variables are being setted to nil automatically at the end of the method.

is incorrect. I think you may be confusing this with weak references which are set to nil when the instance they point to is dealloc'ed.

Note that local variables of objet pointers are set to nil at the beginning of a method.

zaph
  • 111,848
  • 21
  • 189
  • 228
  • Then what should I do when I allocate a local variable in a method, how do I release it? – Ani Nov 08 '13 at 13:17
  • The compiler adds the necessary `release`. Note that `retain`, `release` and `autorelease` are not allowed under ARC, the compiler adds all of these as necessary. The only potential problem is retain cycles and using weak the the answer to that. – zaph Nov 08 '13 at 13:22
  • If you are really curious you can run the allocation tool in Instruments with "Record reference counts" selected and look at what happens. – zaph Nov 08 '13 at 13:26