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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With