Explicitly deleting a shared_ptr

前端 未结 5 1732
遇见更好的自我
遇见更好的自我 2021-02-01 16:37

Simple question here: are you allowed to explicitly delete a boost::shared_ptr yourself? Should you ever?

Clarifying, I don\'t mean delete the pointer held

5条回答
  •  别那么骄傲
    2021-02-01 17:29

    You cannot force its reference count to zero, no.

    Think about what would be required for that to work. You would need to go to each place the shared_ptr is used and clear it.

    If you did force the shared pointer to delete and set it to NULL, it would be just like a weak_ptr. However, all those places in the code using that shared_ptr are not ready for that and expect to be holding a valid pointer. They have no reason to check for NULL, and so those bits of code would crash.

提交回复
热议问题