69. オブジェクト間グローバルマッピング a b c map<void *, Y> g_map; // グローバル y1 y2 y3 share_ptr share_ptr share_ptr share_ptr
70. オブジェクト間グローバルマッピング a b c y1 y2 y3 share_ptr share_ptr share_ptr share_ptr c が消えると… map<void *, Y> g_map; // グローバル
71. オブジェクト間グローバルマッピング a b c 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. マップエントリも所有する a b c y1 y2 y3 share_ptr share_ptr share_ptr share_ptr map<void *, Y>
76. 策2. マップエントリも所有する a b c y1 y2 y3 share_ptr share_ptr share_ptr share_ptr map<void *, Y> struct D{ map<void *, Y> &m_; void operator()(void *p){ m_.erase(p); delete p; } }; D d(g_map); shared_ptr<C> pc(new C(), D(g_map)); g_map.insert(make_pair(pc.get(), Y(…)));
77. 策2. マップエントリも所有する a b c map<void *, Y> y1 y2 y3 share_ptr share_ptr share_ptr share_ptr c を所有する shared_ptr がなくなると…
78. 策2. マップエントリも所有する a b c unordered_map<void const *, Y> y1 y2 y3 share_ptr share_ptr share_ptr share_ptr c を所有する shared_ptr がなくなると… c をキーとするエントリも自動で erase