Can using lambdas as event handlers cause a memory leak?

前端 未结 5 872
被撕碎了的回忆
被撕碎了的回忆 2020-12-08 21:43

Say we have the following method:

private MyObject foo = new MyObject();

// and later in the class

public void PotentialMemoryLeaker(){
  int firedCount =          


        
5条回答
  •  攒了一身酷
    2020-12-08 22:19

    You wont just leak memory, you will also get your lambda called multiple times. Each call of 'PotentialMemoryLeaker' will add another copy of the lambda to the event list, and every copy will be called when 'AnEvent' is fired.

提交回复
热议问题