Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do weak and strong references look like in objective-c?

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?

like image 926
Centurion Avatar asked Sep 05 '11 10:09

Centurion


People also ask

What is strong reference and weak reference in Objective C?

A reference to an object is any object pointer or property that lets you reach the object. There are two types of object reference: Strong references, which keep an object “alive” in memory. Weak references, which have no effect on the lifetime of a referenced object.

What are weak and strong references?

A weak reference is just a pointer to an object that doesn't protect the object from being deallocated by ARC. While strong references increase the retain count of an object by 1, weak references do not. In addition, weak references zero out the pointer to your object when it successfully deallocates.

What are strong references?

strong reference (plural strong references) (computing) a reference that does protect the referenced object from collection by a garbage collector.

What is difference between strong and weak in Swift?

In Swift, a strong reference is the default, for variables, properties, constants, passing into functions (depending on what you do with it), and for closures. With ARC, an instance is only deallocated when its retain count is zero. A strong reference increases the retain count by 1, a weak reference does not.


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).

like image 114
Thilo Avatar answered Oct 24 '22 13:10

Thilo


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.

like image 36
Hitesh Avatar answered Oct 24 '22 12:10

Hitesh