キーワードは 所有 ( ownership ) “…, so keeping track of an object’s ownership is hard work.” – More Effective C++ “Observe the canonical exception-safety rules: Always use the “RAII” idiom to resource ownership and management.” – Exceptional C++ “Containers of raw pointers make manual ownership management tricky, so …” – Modern C++ Design
オブジェクト間グローバルマッピング a bc map<void *, Y> g_map; // グローバル y1 y2 y3 share_ptr share_ptr share_ptr share_ptr
70.
オブジェクト間グローバルマッピング a bc y1 y2 y3 share_ptr share_ptr share_ptr share_ptr c が消えると… map<void *, Y> g_map; // グローバル
71.
オブジェクト間グローバルマッピング a bc y1 y2 y3 share_ptr share_ptr share_ptr share_ptr c が消えると… 無駄なマップエントリ ( デッドマップ ) が発生 map<void *, Y> g_map; // グローバル
72.
策 1. キーを weak_ptr にして適度にクリーンアップ a b c map< weak_ptr<void> , Y> g_map; y1 y2 y3 share_ptr share_ptr
73.
策 1. キーを weak_ptr にして適度にクリーンアップ a b c map< weak_ptr<void> , Y> g_map; y1 y2 y3 share_ptr share_ptr // 以下を適度に実行 for ( auto i = g_map.begin(); i != g_map.end();) { if (i->first.expired()) g_map.erase(i++); else ++i; }
74.
策 1. キーを weak_ptr にして適度にクリーンアップ a b c map< weak_ptr<void> , Y> g_map; y1 y2 y3 share_ptr share_ptr デッドマップが適度に解消される
75.
策2. マップエントリも所有する ab c y1 y2 y3 share_ptr share_ptr share_ptr share_ptr map<void *, Y>