基於效率考量,C++ 的 shared_ptr 和 weak_ptr 沒用 lock 保護。shared_ptr 的實作容易理解:
全部程式都用 shared pointer 存物件的話,可以避免用到 dangle pointer 造成程式掛掉。特別是有用 event loop 時,用 shared pointer 比較不會出錯。但是用 shared pointer 得處理其它問題:
起因是看到 Chromium 的 Smart Pointer Guidelines:
Ref-counted objects — use scoped_refptr<>, but better yet, rethink your design. Reference-counted objects make it difficult to understand ownership and destruction order…