3

I've just read the accepted excellent answer to this question that clarifies the conceptual differences between strong and weak pointers in Objective-C, and I'm still trying to understand the practical differences. I come from a C++ background where these concepts don't exist, and I'm having trouble figuring out where I would use one vs the other.

Could someone please provide a practical example, using Objective-C code, that illustrates the different uses of strong and weak pointers?

Community
  • 1
  • 1

2 Answers2

8

Concept

It's all about the retain counts. ARC is a convenience, to prevent developers worrying about manually retaining and releasing. At its core, a strong variable will knock up the retain count by 1, whereas a weak variable won't.

See below:

@interface Test () {
    NSString* strongVariable; // Instance variables default to strong
    __weak NSString* weakVariable;
}
@end

// This has a retain count of 1, it has been allocated to memory by the user, and stored in a local variable (which is strong)
NSString* str = [[NSString alloc] initWithString:@"Test"];

// The string object will now have a retain count of 2, as the strong variable has increased its retain count
strongVariable = str;

// This weak variable does **not** increase the retain count, and as such it will still be 2
weakVariable = str;

// --

// Now, lets remove some references
// This will reduce the retain count to 1, as a strong variable has lost its link
strongVariable = nil;

// This will also reduce the retain count, as another strong variable has lost it's reference. This means the retain count is 0, and the object can now be considered to not exist
str = nil;

// What happens to weakVariable?
// Because it is referencing an object with a 0 retain count, the runtime will set the value of this variable automatically to nil (iOS 5 and above).
NSLog(@"%@", (weakVariable == nil) ? @"nil" : @"Not nil") // Will print "nil"

You can't get into a situation where a strong variable is referencing an object with a retain count of 0, that defies the core concept of a strong variable. It is worth noting, along side __weak, there is __unsafe_unretained. This acts just like a weak variable, except it isn't automatically set to nil once the retain count reaches zero, meaning it will contain a pointer to a random part of memory (and will crash if you access it, you need to nil it yourself). The reason this exists is due to iOS 4 supporting ARC, but not __weak. In most cases, you'd use __weak.

The above description is just a practical glance, you can read a lot more in depth using this documentation.

Practical applications

Everything is __strong by default. If you want weak, you need to use __weak.

You would typically use weak variables when you conceptually don't want to own a particular object. Whilst a car would own its engine and wheels, it wouldn't own the driver.

Wheel* wheel;
Engine* engine;
__weak Driver* driver;

Conversely, a driver would own the Car.

Car* car;

If the car owned the driver, we would have a retain cycle. The car owns the driver, and the driver owns the car. If we were to release one, what would happen to the other? The whole concept of retain cycles outweighs the scope of this question, but you can read about it here.

The same concept applies to programming patterns, for example delegates. For a table view, the view controller would own the table view, but the table view doesn't own the view controller (Which is uses as a delegate)

//ViewController
UITableView* tableView;
tableView.delegate = self;     

//UITableView
@property (nonatomic, weak) id<UITableViewDelegate> delegate;

Gotchas

One serious use of __weak is within blocks. Without them, you're at serious risk of causing retain cycles without realising. Again, this outweighs the scope of this question, but see here for more information.

Analogy to C++

Within TR1 you have the ability to use shared pointers, these allow you to put a heap allocated object within a stack allocated one, and it manages the memory for us. It does this through the use of reference counting. Everytime you pass the shared pointer to another variable, the reference count is incremented. This analogises to assigning to a strong variable in Obj-C.

WDUK
  • 18,560
  • 2
  • 61
  • 70
1

From what I've found, weak pointers are mostly used for a parent-child relationship, from the child side. By setting the pointer to his parent a weak property, the child will not own the parent and you'll avoid an ownership relationship.

This post helped me a lot to understand these new concepts, you should take a look at it : http://www.raywenderlich.com/5677/beginning-arc-in-ios-5-part-1

micazeve
  • 11
  • 1
  • 2
    There are a lot of uses for weak pointers, for example using references to self/ivars within blocks, delegates, prevent retain cycles, etc...It's more than just about parent-child relationships, albeit that is a very important case. – WDUK Nov 29 '12 at 10:57