Event handler and memory leaks

后端 未结 2 1503
無奈伤痛
無奈伤痛 2020-12-11 19:01

I analyze a VB.NET project and there are some objects (child MDI form) that are disposed, but not removed by the GC.

The MemoryProfiler analysis find, among others,

相关标签:
2条回答
  • 2020-12-11 19:34

    There is another object which references the object that should be removed by the GC via an eventhandler. Meaning: there's another object that is still subscribed to an event of the disposed object.

    You can resolve this by unsubscribing from the event (remove the eventhandlers from the event), when you want to dispose that object.

    0 讨论(0)
  • 2020-12-11 19:47

    The EventHandlerList is used by classes that provide large numbers of events to more efficiently handle memory by only allocating space for the event backing field when it is needed rather than when the event is declared. All events for the main form are therefore stored there.

    I would expect the child form was monitoring when its parent may close (this is a common use-case) and it didn't unsubscribe from the FormClosing or FormClosed event when it received that event. However, that's just a guess. To confirm, look at your child form implementation and find all cases where it subscribes to events from the parent form, then make sure there is a corresponding unsubscription from those events.

    Update
    The menu handler you found that was not removed could well be the root of the issue you see. Try adding the remove call and see if that addresses the leak.

    0 讨论(0)
提交回复
热议问题