Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are strong pointers and weak pointers

I am confused with the notion of "strong pointer" and "weak pointer". Diane Hackborn herself said that:

The object will remain around while there are strong pointers; it is destroyed once the last one is released. All you can do with a weak pointer is comparison and attempting to promote to a strong pointer; the latter will fail if there are no other strong pointers on the object.

Which is quite unclear to me. Is a strong pointer an equivalent of a (boost::)shared pointer? And what is the role of a weak pointer if it is there just to attempt to promote itself to a strong pointer? Like, when do we need weak and strong pointers?

Update:

Thank you everyone, but I'm asking specifically about android's kernel sp and wp, and they have nothing to do with Java's references at all.

Basically I'm trying to crack the code here http://www.androidenea.com/2010/03/share-memory-using-ashmem-and-binder-in.html And don't really understand the use of sp and wp

Update:

The actual answer lies in the comments of the accepted answer. Thanks to Gabe Sechan:

Strong and weak pointers are different smart pointer implementations and do about the same thing- when a pointer goes out of scope, so long as at least one strong pointer references it it will not be freed. If only weak pointers (or nothing) references it will be. The check is done whenever a strong or weak reference to it is descoped.

if I have 10 weak pointers referencing the same object, and one of those 10 goes out of scope, the object will be destroyed? Whereas with strong pointers, only when all 10 of them go out of scope will the object be destroyed?

Yes, almost. If all you have is 10 weak pointers, it would probably have gone out of scope already, when the last strong pointer went out of scope. The implementation may allow it to stick around a little while longer if there's spare memory, but it will be chopped if you go into a low memory condition and it doesn't sound like their implementation is that advanced from her quote. And the use of this is still mainly caching- it is roughly equivalent to a boost shared_ptr and boost weak_ptr. So basically, a weak pointer can have the object it references go away at any time.

like image 980
Max Avatar asked Mar 18 '13 20:03

Max


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 weak pointer?

A weak pointer is a smart pointer that does not take ownership of an object but act as an observer. In other words, it does not participate in reference counting to delete an object or extend its lifetime. Weak pointers are mainly used to break the circular dependency that shared pointers create.

What is the difference between shared pointer and weak pointer?

The only difference between weak_ptr and shared_ptr is that the weak_ptr allows the reference counter object to be kept after the actual object was freed. As a result, if you keep a lot of shared_ptr in a std::set the actual objects will occupy a lot of memory if they are big enough.

How does a weak pointer work?

It decreases the "use count" to zero, and begins executing the deleter. Now the weak_ptr decreases the "weak count" to zero, and finds the "use count" is zero as well. So it deletes the "counter" object, and with it the deleter. While the deleter is still running.


1 Answers

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.

First, take a quick look at the sp implementation in StrongPointer.h.

It is simply a wrapper for reference counting. For example,

template<typename T> template<typename U> sp<T>& sp<T>::operator = (U* other) {     if (other) ((T*)other)->incStrong(this);     if (m_ptr) m_ptr->decStrong(this);     m_ptr = other;     return *this; } 

If you create a Strong Pointer by sp<IBinder> strongPointer, the m_ptr is the referenced object. As you can see in the source code, the sp template only represents a strong pointer so that system won't free the memory as long as I hold this sp. It doesn't maintain a reference counter. The counter is maintained in RefBase class. And in order to use the StrongPointer, your obj need to be an instance of RefBase.

RefBase class maintains both strong reference counter and weak reference counter, the only difference is the referenced object will be freed if the strong counts to 0. Moreover, for an object managed by Refbase, it may referenced by some Strong Pointers and Weak Pointers simultaneously.

You can see a widely uses of StrongPointers in Android framework, most of them are on IBinder object, a native binder object can passed through different processes. Different processes can hold strong pointers to a same object, the object won't be revoked by system as long as one process are still holding the pointer.

like image 73
StarPinkER Avatar answered Sep 29 '22 10:09

StarPinkER