Can we dispose viewModels by removing views from Regions?

Topics: Prism v2 - Silverlight 4, Prism v4 - Silverlight 4
Aug 24, 2010 at 4:05 PM

I am wondering when we remove a view from a region, does it also dispose viewModels associated with them [In View-First approach I guess] Or do we explicitely need to dispose the presenters, controllers and viewModels.

Thanks,

Ravi

Aug 24, 2010 at 5:24 PM
Edited Aug 24, 2010 at 5:27 PM

Hi Ravi,

Beyond of the presentation pattern of your choice, MVVM, MVP, etc,  if you remove a view from a region, it will be disposed as well as its objects (e.g. a View Model) when the garbage collection occurs. That said, take into account that the garbage collector will dispose all these components successfully if you don’t have any pointer alive to your objects from other place.

Therefore, it is not necessary to explicitly dispose a view model. On the other hand a memory leak has been detected and treated in the Damian Schenkelman’s blog:

For more information about this topic, you could take a look at the following forum threads:

Please let me know if this helps.

Fernando Antivero
http://blogs.southworks.net/fantivero

Aug 24, 2010 at 6:14 PM

Fernando,

Thanks for you quick reply. That was definitely helpful. It addresses 90% of my current issues regarding view disposal because most of them I am using the View-First approach to resolve Views.

I have few cases where a Presenter is dynamically generating UI components and binding them to a view. To make matters worse, there are events hooked between the UI components and the presenter. I don't think I am handling the disposal of the presenter well enough because all I am doing is setting the reference to the presenter to Null and it seems the instance of it still lingers around because it keeps handling some UI events. Long story short, I guess I am wondering if there is a best practice regarding how to handle disposal in case of Model-First or Presenter approaches like mine because just removing the views from the region doesn't seem to cut it for these cases [or I might be totally misunderstanding something here :-) ]

Thanks again for your time,
Ravi

Aug 24, 2010 at 7:37 PM
Edited Aug 24, 2010 at 7:49 PM

Hi Ravi,

There is no specific guidance about memory management and dispose patterns topics in Prism out-of-the-box. In most usual View-First cases, there is no need to do any special disposing. By the way, this topic requires you to evaluate in advance what your classes will use to know whether it requires to induce the garbage collection or not.

Although View-First or other approaches do not require any specific memory management, your application do. So, I would like to share the following links to generic content on this subject:

On the other hand, if you manage an event strong references between two objects, it could be necessary to untie them to get it disposed correctly.

Additionally, you could take a look at the following documentation section on MSDN:

Please let me know if this helps.

Fernando Antivero
http://blogs.southworks.net/fantivero

Aug 24, 2010 at 8:54 PM

Fernando,

Thanks again. This is definitely helpful and points me in the right direction.

-Ravi