Removed element still listens to events

Posted on 2011-09-19
Last Modified: 2013-11-12
Hello there

I got a canvas on what I add some UIElements like this:

private void AddChild(UIElement child, Canvas parent)
    // first I remove all the other children

    // then I add the new one

Open in new window

I add my new field on this canvas like this:

private void AddField()
    AddChild(new FieldView(), MainCanvas);

Open in new window

So I have no other reference to that FieldView, but on the canvas.

This FieldView listens to some events and shows some MessageBoxes. Now, when I add a new FieldView on the MainCanvas, the only reference of the old one should be removed (if I understand this parent.Children.Clear() right).

But it seems like it's not really collected by the GarbageCollector, because now, I always get 2 MessageBoxes on these events (if I add a new field again, I get 3 MessageBoxes and so on).

Can anyone of you tell me, why the old FieldViews aren't removed? Is it just because they still listen to those events or doesn't parent.Children.Clear() really remove them?

Thank you for your help!
Question by:innovasoft
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Expert Comment

ID: 36564907
Is the event that FieldView listens to static?

Expert Comment

ID: 36565191
If you are wiring up the event handler in code, you may have to "unwire" the event handler first. Example:
var someElement = whatever;

// unwire event handler if there is one
someElement.SomeEvent -= this.someEventHandler;

// wire event handler
someElement.SomeEvent += this.someEventHandler;

Open in new window

Not sure of your situation, but if you are using any IoC or dependency injection type stuff, then you'll need to be sure your Views and FieldView things are configured with the appropriate lifetime for the containers, so instances of certain controls don't "hang around" after you think they are dead. If you are not using anything fancy like that, then disregard this comment :)

Hope this helps you narrow down the issue.


Expert Comment

ID: 36566345
You are newing up your FieldView class and adding without cleaning the listeners from the previous FieldView.

That means the event to which the FieldView registered holding the instance of your old FieldView object.

So the GC can't release your FieldView object as they are registered to your event, even though the FieldView is removed from the Canvas.

I don't your code but this is what you could try

before adding a FieldView to the event for call back, clear the event and add the new FieldView object for listening
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.


Author Comment

ID: 36566945
thank you all for your answers.

I see now, that the object still exists, because it's registered to the event. So I have to unwire them, when I remove the Control.

unfortunately, the way, smickle wrote, doesn't work, because it only unwires the listeners of the new class before it re-registers them again.

So, I had the idea to write a dispose-method where all the events will be deregistered. The problem is, when I replace the


Open in new window


for (int i = parent.Children.Count - 1; i >= 0; i--)
    UIElement el = parent.Children[i];

    if (el is IDisposable)


Open in new window

it takes hours to remove the field. Is there a nicer way? Maybe I should do this with parallel tasks?

Author Comment

ID: 36566956
by the way:

@jagrut: no, the events aren't static

@smickle: no, I don't use any dependency injection stuff

Expert Comment

ID: 36570777
For your Dispose idea, you may have to just unwire the event in your for loop, if it's possible in scope of this for loop. If the event handler is the only thing that is "holding on" to the instance, then GC should cleanup.

I've had issues before that were similar, but mine involved having to explicitly setting the .Content property of my UIElement to null and unwiring event handlers. But that was SL3 stuff. If you are in Silverlight, then there's a known issue, a memory leak, that is kinda sorta related to all of this. It is being fixed in SL5. The known issue is only in SL and not WPF. So if you are in WPF, then you can rule that out.

Here is info on the known bug in SL that causes memory leaks:


Accepted Solution

innovasoft earned 0 total points
ID: 37043790
actually, I couldn't really fix this. So I just loop through all the children now and dispose them seperately. In the dispose-method, I deregister all the events.

Not nice, but it works.

Author Closing Comment

ID: 37068277
didn't solve the problem

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
ODBC Connection; Switch between 2 SQL Databases VBA 11 127
Need more details 5 183
SSRS troubles 4 83
Shared files and folders migration 2 27
This article shows how to make a Windows 7 gadget that accepts files dropped from the Windows Explorer.  It also illustrates how to give your gadget a non-rectangular shape and how to add some nifty visual effects to text displayed in a your gadget.…
This article shows how to make a Windows 7 gadget that extends its U/I with a flyout panel -- a window that pops out next to the gadget.  The example gadget shows several additional techniques:  How to automatically resize a gadget or flyout panel t…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA.…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question