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.
An essential concept in ARC is the retain count, which is a number that keeps track of how many objects are “holding onto” to another object. ARC only applies to reference types such as classes, and not to value types like structs. Value types are copied, so they don't work with references.
ARC differs from tracing garbage collection in that there is no background process that deallocates the objects asynchronously at runtime. Unlike tracing garbage collection, ARC does not handle reference cycles automatically.
Automatic Reference Counting (ARC) is a memory management option for Objective-C provided by the Clang compiler. When compiling Objective-C code with ARC enabled, the compiler will effectively retain, release, or autorelease where appropriate to ensure the object's lifetime extends through, at least, its last use.
If I remove one of the
Child
objects from my school (say by making itsproperty = nil
, or by changing my property to point at a new object), will itsBook
be properly released?
Yes, it will be released as long as there are no other strong references to it.
What do I have to do to make sure that this is the case?
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 Book
has a strong back-reference to Child
, either make that reference weak
, or clear it out at the same time as you set your reference of Book
to nil
(the second option is error-prone, and therefore is not recommended).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With