Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Objective-C Basics (Retired) Advanced Objective-C Memory Management

Why we use memory directives?

In the Video, It explained when to use memory directives. But didn't explain Why should i use them. I want to understand why do we use Strong or weak or copy? What does it mean that i want to maintain the reference count. Why should I care about the reference count.

Thank you

4 Answers

A reference count keeps your object alive. Otherwise the OS will garbage collect it. It's the same way in Java. Think of it as a Hollywood star that can only stay alive if at least one person is watching them. If no body watches them anymore, they die.

So knowing that, you know the purpose of strong and weak and why they are implemented. You can also determine that having two objects that have strong pointers to one another is also a bad idea because then neither one of them will ever die and free up their memory back to the system.

Stone Preston
Stone Preston
42,016 Points

Ray wenderlich has a nice little explanation of ARC concepts here. ARC is a bit confusing honestly, and is probably a bit out of the scope of a beginner course such as Objective-C basics. But the article I linked has some nice images that illustrate the concepts that make it a bit easier to grasp

Yeah what micheal Bowen said ;)

Andre Kovac
Andre Kovac
5,588 Points

Hi,

the Guide from Ray Wenderlich (mentioned by Stone Preston) already helped me to get a better understanding of pointers and memory allocation, but I still didn't get the parent-child relationship with strong and weak yet.

Let's take the example from the video: Shape has a weak reference to Button and Button has a strong reference to Shape.

So when I write the following in main.m, could someone please comment whether my thoughts in-between the code lines are correct:

Shape *shape1 = [[Shape alloc] init];
Shape *shape2 = [[Shape alloc] init];
Button *my_button = [[Button alloc] init];

Objects shape1, shape2 and my_button each are strong pointers to not yet specified values? Or does there have to be a value specified in order for the pointer to point to sth?

my_button.shape = shape1;

The shape property of my_button now has a strong reference to shape1?

my_button.shape = shape2;

The shape property in my_button changed. Thus, the memory space occupied by the shape1 object is freed in memory?

shape1.button = mybutton;

What does this weak reference actually mean here? What is it good for? Do we need it?

=================

Further questions:

  • In the example in the video Shape is the parent, right? Why does it then only have a weak reference?
  • Why would Shape have a reference to Button in the first place? Is it just for the sake of having an example of weak and strong references?

================

Thanks in advance!