質問

I've looked through the questions on strong/weak references, and understand the reason for using weak (the parent to child relationship). However, I'm confused about specific scenarios where a parent to child relationship is created.

For example, is adding subviews to a UIView object..an example of creating a parent/child relationship? What is?

So far, I did everything in my project using strong, nowhere have I used weak, but I'm not sure if I'll run into memory management issues (or how to even check if I will).

Can anyone provide specific situations or examples where a parent to child relationship is created?

Thanks!

EDIT: In fact, I am getting some "Received Memory Warning" problems in one of my ViewControllers that displays a lot of data (map view, number of images, text, buttons). Everything property has a strong pointer. I need to fix my memory management issues for this ViewController

役に立ちましたか?

解決

You're understanding is backwards. Weak references are more often used for implementing child-to-parent relationships. They would seldom make sense for a parent-to-child relationship. Generally the parent owns the child; that means strong.

The vast majority of the time you want a strong reference. That's why it's the default. The most common reason not to have a strong reference is if it would cause a retain loop. For instance, if A has a strong reference to B, then if B had a strong reference to A you'd have a loop and neither object would ever be deallocated. So you pick one of the objects to be the owner, and it has the strong reference. The other object has a weak reference.

The most common case of this is delegation. A delegate almost always owns the thing it is delegate for. So the delegating object should have a weak reference to the delegate. As a convention in Objective-C, a property called delegate is expected to be weak. (If this feels backwards, think about how you use UITableView and UITableViewDelegate in practice, and which one you'd want to consider the "owner.")

Weak delegate pointers are not a hard-and-fast rule. There are exceptions such as NSURLConnection. If the delegating object has a shorter lifetime than the delegate, then it is ok (and generally preferable) for it to maintain a strong reference.

"Received Memory Warning" does not necessarily have anything to do with memory management. It just means you're using too much memory. If you have retain loops, then you may be leaking memory, and that would cause this warning. But it could also be because you're simply using too much memory. The "Allocations" tool in Instruments is the best way to investigate this.

While the implementation of "strong" and "weak" are very recent additions to Objective-C, they just formalize and provide better language support for what properly written code has been doing for many years with manual retains. The ownership patterns are identical today to what they were before ARC.

他のヒント

Some people put together a very helpful diagram explaining when to use weak references on the "Coding Together" Piazza class. It has some great diagrams explaining basic memory management with strong/weak pointers.

http://piazza-uploads.s3-website-us-east-1.amazonaws.com/attach/h3ex5vh7htrh9/h4777mrpwp17bg/h4777tczi147de/Views,%20Outlets,%20Weak%20References.pdf

When you add a subview to a view, the parent will retain it's subview under the covers. Yes this is a parent child relationship. If you are creating your own UIView subclasses, you will be doing the same.

Parents have a strong relationship with their children, but children should never have a strong relationship on their parents. This can lead to retain cycles, where neither can be released because they both have a strong connection with one another.

Strong is the default, you will tend to use weak when you are using the delegate pattern.

Maybe you should post more specific info about the problem you are having with the received memory warning, as this question is quite general about memory semantics.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top