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()
   
{
        timer
= new System.Timers.Timer();
        timer
.Interval = 100;
       
//timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
        timer
.Start();
   
}

   
public void Dispose()
   
{
       
Dispose(true);

        GC
.SuppressFinalize(this);
   
}

   
protected virtual void Dispose(bool disposing)
   
{
       
if(!this.disposed)
       
{
           
if(disposing)
           
{
               
// Dispose managed resources.
                timer
.Dispose();
                timer
= null;
               
//GC.KeepAlive(timer);
               
//GC.Collect();
           
}

            disposed
= true;
       
}
   
}

   
~MyViewModel()
   
{
       
Dispose(false);
   
}
}

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?

 

Original: http://stackoverflow.com/questions/9174898/memory-leak-with-idisposable-in-viewmodel-why

Developer
Feb 7, 2012 at 8:09 PM

Hi,

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.

Thanks,

Guido Leandro Maliandi
http://blogs.southworks.net/gmaliandi

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 (http://msdn.microsoft.com/en-us/library/ee155691.aspx) 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: http://mef.codeplex.com/wikipage?title=Parts%20Lifetime&referringTitle=Guide

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.
http://stackoverflow.com/questions/9174898/memory-leak-with-idisposable-in-viewmodel-why