WeakReference and event handling

前端 未结 4 2166
鱼传尺愫
鱼传尺愫 2020-12-14 00:53

Is it a good practice to implement event handling through WeakReference if that event is the only thing holding the reference and that we would need the object to be garbage

4条回答
  •  萌比男神i
    2020-12-14 01:24

    Weak delegate pattern is something that should be there in CLR. Normal events exhibit "notify me while you are alive" semantics, while often we need "notify me while I'm alive". Just having delegate on WeakReference is wrong, because delegate is an object too and even when recepient is still alive and have incoming references, delegate itself is only being referenced by said WeakReference and will be collected instantly. See this old post for an example of implementation.

提交回复
热议问题