I have a quick question about ARC in iOS. (Sorry I’ve asked so many of these types of questions, but I’m just sooo confused regarding memory management.). It’s important to note that I’ve never used the old memory maintenance system (retain, release, assign…etc) so I don’t really know what those terms mean.
Right now I’m confused regarding what I have to do to make sure that strong properties get released properly. For example, suppose I’m making a school app and my School object contains strong property references to 5 different Child objects (not in an array). Each Child object has a strong pointer (property) to a Book object.
If I remove one of the Child objects from my school (say by making its property = nil, or by changing my property to point at a new object), will its Book be properly released? What do I have to do to make sure that this is the case? Do I need to write self.myBook = nil in a dealloc method? What if Child was a View Controller, would I need to write self.myBook = nil in the viewDidUnload method?
I’m targeting only iOS 5 (and up) so the old way of memory management doesn’t really matter to me.
Yes, it will be released as long as there are no other strong references to it.
Nothing in particular: ARC will decrement object’s reference count when you set the reference to that object to
nil, see that the object is no longer referenced, and proceed to deleting it. It is smart enough to deal with the items referenced from the object being deleted, recursively, so you are not going to leak any memory.One thing you have to worry about is circular references: if your
Bookhas a strong back-reference toChild, either make that referenceweak, or clear it out at the same time as you set your reference ofBooktonil(the second option is error-prone, and therefore is not recommended).