Can delegates cause memory leaks?

Can delegates cause memory leaks?

I mean, for example, if class A contains ADelegate , and the latter points to BMethod (class B ), can this prevent the collection of class A or B classes using GC?

If so, how can we β€œfree” delegates (setting ADeletate = Nothing / null?)

How do you comment on this:

 //Class A Finalize, containing ADelegateInstance as ADelegate' protected override void Finalize() {  ADelegateInstance = (ADelegate)System.Delegate.RemoveAll( ADelegateInstance, ADelegateInstance);  ADelegateInstance = null;  base.Finalize(); } 'Class A Finalize, containing ADelegateInstance as ADelegate' Protected Overrides Sub Finalize() ADelegateInstance = _ CType(System.Delegate.RemoveAll(ADelegateInstance, ADelegateInstance), _ ADelegate) ADelegateInstance = Nothing MyBase.Finalize() End Sub 
+4
source share
5 answers

Yes, the link will remain alive if you do not unsubscribe from the event:

 someObject.SomeEvent -= SomeDelegate; 
+8
source

AFAIK, the context referenced by closure / delegation cannot really be garbage collected while still referencing closure / delegation, otherwise it will lose its context.

Taking an example for this answer , we see that the delegate can refer to the inneri variable in the context of the object. Thus, the object that actually contains inneri cannot be garbage collected until the delegate no longer refers, in this case, until the garbage Button is collected by Button .

 for (int i = 0; i < 7; i++) { var inneri = i; Button newButton = new Button(); newButton.Text = "Click me!"; newButton.Click += delegate(Object sender, EventArgs e) { MessageBox.Show("I am button number " + inneri); }; this.Controls.Add(newButton); } 

Related posts:

+2
source

Just having a link is not enough to cause a memory leak. Consider the following.

If a stream generates 3 objects (where β†’ denotes a link), A β†’ B β†’ C β†’ A

If A does not refer to a stream, all of them are collected. Circular links are reviewed by GC.

However, this also means that if a delegate contains a reference to an object and the object is still referenced with the delegate, the delegate function will not be cleared.

This will give you the following below.

A - (object with a delegate) B - object containing a reference to the function.

When A falls out of scope, then B will be.

+2
source

If A contains a function delegate in B, then A will not be destroyed by GC.

It is a good idea to always put "mydelegate - = B.method" every time you write "mydelegate + = B.method".

Although this is not a true memory leak, as objects can still be reached.

+1
source

There was a situation when a singleton was used in an ASP.NET application. And for some reason, he subscribed to control events. The fact that it was a singleton (containing a link to itself) did not allow the GC to collect it, on the other hand, that singleton never deleted subscriptions to audit events. This caused a constant increase in memory consumption: the controls used to serve a single request that the GC does not clear due to an existing singleton link, new controls created for each new request.

0
source

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


All Articles