When to use weak pointers (WP) in the Android native framework (AOSP)

I know the difference between SP and WP. SP will automatically re collect dynamically allocated memory. But I don't know when and how to use WP? Who can give an example?

resolvent:

You must know that using SP will increase the reference count of the object, but using WP will not. So WP is only an address store. It cannot be used to access the field of the object unless you promote it. However, if the object has been released, WP. Promote() will return a null pointer

Therefore, WP will be mainly used in scenarios where you want to have a reference cache of a memory object but do not want to keep it. You can access data by raising WP each time. If the object is no longer available, you need to (request some other code) create it again

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>