Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Weak/Strong Reference Pointer Relationship

I have been attempting to write my own weak/strong pointer's but I am not clearly understanding the relationship. Everything I seem to come across does not make it and clear and quite often one doc will contridict what another doc says. Could anyone please explain the weak/strong pointer relationship in detail, with maybe an image or code sample also?

(Please do not just tell me to "use boost" or "use tr1", etc. This is not homework, I want to learn).

like image 949
chadb Avatar asked Mar 20 '11 19:03

chadb


People also ask

What is the difference between strong and weak pointers?

sp means StrongPointer in Android, the memory that occupied by the pointed object will be freed if the reference count equals to 0. wp means WeakPointer, so if I have a weak pointer, I don't care whether the referenced object is alive or not. It might be used in some cache and comparison scenarios.

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 is a weak reference in C++?

Weak references and breaking cycles (C++/CX)A WeakReference object supports the Resolve method, which returns null if the object no longer exists, or throws an Platform::InvalidCastException if the object is alive but is not of type T .

What is the difference between strong and weak references in IOS?

strong is the default. An object remains “alive” as long as there is a strong pointer to it. weak specifies a reference that does not keep the referenced object alive. A weak reference is set to nil when there are no strong references to the object.


1 Answers

A strong pointer owns the underlying raw pointer. Its existence can keep the raw pointer alive, and as a result it can't point to something that's been cleaned up. Its destruction can cause a delete on the raw pointer. A weak pointer only knows the raw pointer, and whether or not it's still valid. It doesn't keep the raw pointer alive by existing and it can't make the raw pointer go away by being cleaned up.

To get a more detailed answer you would need to show the contradictions you've found or the question that you have about a specific part of the definition.

like image 144
Kate Gregory Avatar answered Oct 26 '22 01:10

Kate Gregory