Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

when to use weak pointer (wp) in android native framework (AOSP)

I know the different between the SP and WP, and the SP automatically recollect the dynamically allocated memory. But I do not know when and how will the WP be used? Can anyone give me an example?

like image 621
bpceee Avatar asked Aug 20 '13 03:08

bpceee


1 Answers

You must know that using a SP will increase the ref count of an object, while using a WP won't. So WP is just a store of address, it cannot be used to access the fields of an object, unless you promote it. But if the object is already released, wp.promote() will return a NULL pointer.

So, WP will be mostly used in the scenarios that you want to have a reference cache of a memory object but do not want to hold it. You access the data by promote your WP every time, and if the object is not available any more, you need to (ask some other code to) create it again.

like image 106
Robin Avatar answered Sep 21 '22 04:09

Robin