Wikipedia states "In computer programming, a weak reference is a reference that does not protect the referenced object from collection by a garbage collector". How do those two types of references look like in code? Does a weak reference is a reference made by an autoreleased message?
2 Answers
The following answer is for the case when there is no garbage collection (such as on iOS).
In the case of garbage collection, there is actually a keyword (__weak
) to create a weak reference.
A "weak" reference is a reference that you do not retain.
You need to use these weak references to break up cycles. A common case is a child object that needs a reference to its parent object. In this scenario, the parent would retain a reference to the child object, and the child object has a reference to its parent, but does not retain it. This works because the child object only needs to exist as long as the parent object does.
Does a weak reference is a reference made by an autoreleased message?
Not really, that would be a "very weak reference" ;-)
Auto-released stuff goes away when the call stack is unwound (at the end of every event loop for example). If you need anything to be less temporary, you need to retain a reference (or like in the case above be sure that some other part retains it sufficiently).

- 257,207
- 101
- 511
- 656
-
I'm mostly concerned about iOS SDK. Regarding iOS 4: so, do all "assign type" properties and any other simple assignments of one obj pointer to another without explicit retain message should be considered as weak references? – Centurion Sep 05 '11 at 10:55
-
1Yes. But note that methods like "alloc" include an implicit "retain". – Thilo Sep 05 '11 at 11:08
-
@Thillo If we are using properties, is `weak` is same as `assign` ? – M S Jun 14 '12 at 13:34
A weak reference is a reference that isn't strong enough to force an object to remain in memory while a strong reference forces an object to remain in memory.
If you have created weak reference to any variable, you may get nil for that.
UITableViewDelegate
, UIScrollViewDelegate
, etc are examples of weak references.
Example of Strong reference :
MyClass *obj1 = [[Myclass alloc] init];
Myclass *obj2 = obj1;
Here obj2
has strong reference to obj1
mean if you remove obj2
from memory then obj1
also get removed.

- 22,812
- 8
- 71
- 144

- 1,230
- 1
- 10
- 12
-
1Does obj2 really has a strong reference to obj1? You state "A weak reference is a reference that isn't strong enough to force an object to remain in memory", and looking at your example, sending a release message to obj1 wouldn't prevent the obj1 from being retained in memory because "Myclass *obj2 = obj1;" assignment does not increase retain count on obj1. IMHO it's a weak reference. – Centurion Sep 05 '11 at 10:51
-
-
I think, a second statement "[obj2 retain];" would make it a strong reference. Yes, obj2 is just a pointer to obj1 and both 2 retain counts belong to the same object allocated in memory and IMHO stored in one place somewhere. But every +1 retain count has an owner (at least theoretical) and for me it's better to think about that owner as var/ivar/property that was used to sent an explicit (retain) or implicit (alloc) retain message on the target object. – Centurion Sep 05 '11 at 11:53
-
Retain any object means you are taking ownership of that object right?There is no point of any reference!! – Hitesh Sep 05 '11 at 12:34