Memory leak with IDisposable in ViewModel, why?

Topics: Prism v4 - WPF 4
Feb 7, 2012 at 5:16 PM

Using a C#, WPF, MVVM, Prism, MEF. I need the external effects (I have it from the View is called Dispose ()) to free resources in the ViewModel, used in the View. I use something like the following code:

public class MyViewModel: IDisposable, IMyViewModel, ...
private bool disposed = false;

private System.Timers.Timer timer;

public MyViewModel()
= new System.Timers.Timer();
.Interval = 100;
//timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);

public void Dispose()


protected virtual void Dispose(bool disposing)
// Dispose managed resources.
= null;

= true;


As such, the View and ViewModel instances are not destroyed and there is a memory leak. If you delete the interface and its methods IDisposable, the View and ViewModel deleted successfully.

I using ANTS Memory Profiler. On next diagram you can see three instance, from DisposableReflecationComposablePart, but must be a one instance.

enter image description here

I can not understand what was happening. Any ideas?



Feb 7, 2012 at 8:09 PM


From the information you're providing here, I can't think of an explanation for the memory leak caused by implementing IDisposable.

As mentioned in the original question you linked above, we believe it isn't necessary to implement IDisposable in your case, since you don't seem to be using unmanaged resources in your scenario.


Guido Leandro Maliandi

Feb 8, 2012 at 1:04 AM

The above code is just an example of course means the availability of resources to be freed.

Feb 8, 2012 at 5:25 AM
Edited Feb 8, 2012 at 5:26 AM

I found an article ( which states that non-shared-export should be used to destroy ReleaseExport exported object.
There is a good example on this subject?

Feb 8, 2012 at 12:10 PM

I understand that my statements explain the problem is the link:

in the following sentence:
Thus, the container will not hold references to parts it creates unless one of the following is true:

     The part is marked as Shared
     The part implements IDisposable
     One or more imports is configured to allow recomposition

Feb 9, 2012 at 5:29 PM

Workaround found: the use of its interface.