Garbage collector and event handlers

后端 未结 2 1012
無奈伤痛
無奈伤痛 2021-01-02 11:44

A quick question. Say that I have a class implemented as in below example.

class Subscriber
{
    private Publisher publisher = new Publisher;


    public S         


        
2条回答
  •  梦谈多话
    2021-01-02 12:08

    It wouldn't cause a leak - the GC can handle circular references with no problems.

    However, it would mean that the publisher would effectively have a reference to the subscriber, so the subscriber couldn't be garbage collected until either the publisher is eligible for GC, or it unsubscribes from the event.

提交回复
热议问题