Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我知道SP和WP的区别,SP会自动回收动态分配的内存。但我不知道何时以及如何使用 WP?谁能给我一个例子?
您必须知道使用 SP 会增加对象的引用计数,而使用 WP 不会。所以 WP 只是地址的存储,它不能用于访问对象的字段,除非你提升它。但是如果对象已经被释放,wp.promote()就会返回一个NULL指针。
wp.promote()
因此,WP 将主要用于您希望拥有内存对象的引用缓存但又不想持有它的场景。您每次都通过提升您的 WP 来访问数据,如果该对象不再可用,您需要(要求其他一些代码)再次创建它。