Why doesn't Visual Studio Windows Form Designer code cause a memory leak?

As I understand it, one of the main causes of memory leaks in C # is the refusal to register event loggers when placing it. For this reason, whenever I manually register an event - for example, Timer.Elapsed += ... - I am sure Timer.Elapsed -= ... when I ended up with an object (or a parent object).

However, I just looked at the created Windows Form Designer class and noticed that although it happily accepts events (for example, this.button1.Click += new System.EventHandler(this.button1_Click); ), it seems that the cleaning procedure is missing , except for the default action components.Dispose(); .

Does this mean that the Dispose() method of each component should unregister / unsubscribe from any events related to it; if so, how does the component not register from "external" event handlers that it does not know about, and does this mean that manual attempts to remove event listeners from standard [IDisposable] Windows controls (timers, buttons, forms, etc.) .d.) usually unnecessary?

thanks

+6
source share
3 answers

Event handlers only cause a memory leak if the object containing the event lives longer than the object containing the handler.

In a typical WinForms scenario, both the controls and the form code only live until the form opens, so there is no problem in the first place.

You only need to unregister your handlers from static events, single or other long-lived objects.

+6
source

Good design, basically. The object model was well designed to ensure that the event source could not survive the subscriber. There is, of course, a circular link, the form saves the link to the control through its Controls collection, as well as a possible private variable, the control adds a link to the form through an event subscription. But the control lifetime is controlled by the form, both disappear when the user closes the window. This removes the usual single reference to the form object stored in the internal table, which maps the handle to the form. GC has no problems with circular links.

There are a few sharp edges, Application.Idle and SystemEvents events are the problem. They have a yellow ribbon in the MSDN library.

Utilization is also automatic, it is not used to unsubscribe from events in Winforms, each control provides links in its Controls collection. This starts with the Form class and automatically repeats through the tree. Overriding the form The Dispose () method is unusual, it also tends to cause a lot of anguish because this method is present in the form's Designer.cs file. Moving this method is fine, as the FormClosed event is used to place as an alternative.

It has a sharp edge with a chainsaw byte. Disposal of controls is not optional in Winforms. Very unusual, this is not necessary in everything else within the framework, and the finalizer refuses to forget to name it. Not in Winforms, if you use Controls.Clear or Remove, then the control that you remove is not deleted. It redraws into a hidden window called the "parking window." Keeping control to move the other parent. Good feature unless you move it elsewhere. He will live on this hidden window forever, a very nasty leak. Not a good design.

There are templates for solving the life time problem for events. The "weak event pattern" is rather arbitrary in .NET programming. This is a sign of the design problem bit signature, usually caused by sympathy for the observer pattern, because it works well in .NET, but does not like the contract that comes with it. An object-oriented model is almost always the root of the problem, for example, an abbreviation of three letters, the name of which should not be mentioned in the [winforms] tag :)

+3
source

The event source will not support the subscriber. When the form leaves, it will be eligible to participate in the GC, which forces students to become eligible.

+2
source

Source: https://habr.com/ru/post/956982/


All Articles