Is an unwanted event a memory leak?

If yes, is there a way to release it globally for all wired events

Edit: Say for example. I have objects, each of which is marked with an event like orm.NatureChanged += Nature_Changed; . I mark these events when I create each instance of orm. If I had not refused, for example, orm.NatureChanged -= Nature_Changed; Will it cause a memory leak?

+6
c #
source share
4 answers

No, because when you release an event, the delegate (this is the object) that was associated with the event is no longer rooted and will be assembled when the GC considers it appropriate. This assumes that, of course, the event delegate is not bound to multiple handlers, in which case it will not be assembled until it is freed from all events.

+5
source share

No matter what you ask, the technical answer to your question is no. Technically, if you don't find an error in the CLR, there are no true “memory leaks” with managed objects (which is most of what makes them a good thing). To answer what I think you are really asking, it looks like you are asking one of two things:

  • Is there something that needs to be done with events in which there are no delegates attached to them?
  • Can an event prevent garbage collection from clearing objects?

The answer to the first question is simply no. Move on, nothing is visible here.

The answer to the second is discussed in detail here in SO and other areas of the network. The short version is that a related event handler means that the GC will treat the target instance as "reachable" by the event instance. This can lead to the fact that objects remain in memory longer than expected, since this availability is somewhat transparent to the user (developer) due to the way delegates are created.

In other words, let's say I have two objects: Producer and Consumer. The producer fires an event that the consumer consumes.

 public class Producer { public event EventHandler OmgIDidSomething; } public class Consumer { public void AttachTo(Producer producer) { producer.OmgIDidSomething += new EventHandler(producer_OmgIDidSomething); } private void producer_OmgIDidSomething(object sender, EventArgs e) { // ... } } 

In this example, any Consumer instance where AttachTo is called will remain available to the GC as long as the Producer instance to which it is attached does not have the right to collect, because the delegate for the implementation of the OmgIDidSomething event has a reference to the Consumer instance that matches.

+6
source share

This is not a memory leak, it simply does not bind handlers to this event if they are not connected, automatically or otherwise. Thus, the event does not shoot anyone, it is cleared, and life goes on.

This talk talks about this issue: How do events cause a memory leak in C # and how do weak links help mitigate this?

Note the following help information: What does the property of the AutoEventWireUp page mean?

+2
source share

If you meant that events that do not receive unwired can cause a memory leak, the answer is that it can, if the actual lifetime of the object containing the event delegate is much longer than the useful life of the object to which the delegate belongs. For example, if the collector intercepted the CollectionChanged event, and someone had to list it, but each time the collection was enumerated (without disposing of the enumerator), a new enumerator object would be created that would remain in memory for as long as the base collection .

+2
source share

All Articles