When to use weak pointer (wp) in native Android environment (AOSP)

I know the difference between SP and WP, ​​and SP automatically remembers dynamically allocated memory. But I do not know when and how WP will be used? Can someone give me an example?

+4
source share
1 answer

You should be aware that using SP will increase the number of references to an object, while using WP will not. Thus, WP is just a repository of addresses; it cannot be used to access the fields of an object if you are not advertising it. But if the object is already released, wp.promote() will return a NULL pointer.

Thus, WP will be mainly used in scripts in which you want to have a reference cache of a memory object, but do not want to hold it. You get access to the data, each time advertising your WP, and if the object is no longer available, you need to (ask for another code) to create it again.

+3
source

Source: https://habr.com/ru/post/1497907/


All Articles