IRegionMemberLifetime is Not working

Topics: Prism v4 - WPF 4
Sep 20, 2013 at 10:23 AM
Edited Sep 20, 2013 at 10:24 AM
//ViewModel
 [Export(typeof(IRegisterDetailsViewModel))]
    [PartCreationPolicy(CreationPolicy.NonShared)]
    public class RegisterDetailsViewModel : PrincipalViewModelBase, IRegisterDetailsViewModel,IRegionMemberLifetime 
    {

  Boolean IRegionMemberLifetime.KeepAlive
        {
            get
            {
                return false;
            }
        }
}

//View

[Export("RegisterDetailsView", typeof(IRegisterDetailsView))]
    [PartCreationPolicy(CreationPolicy.NonShared)]
    public partial class RegisterDetailsView : UserControl, IRegisterDetailsView
    {
        [Import]
        public IRegisterDetailsViewModel ViewModel
        {
            get
            {
                return this.DataContext as IRegisterDetailsViewModel;
            }
            set
            {
                DataContext = value;
                ViewModel.View = this;
            }
        }

}
Hi,

Actually When I am implementing the IRegionMemberLifetime for the above viewmodel and view, I am accessing the view in the viewmodel through this code "ViewModel.View = this;"


At this scenario IRegionMemberlifetime is not working, the memory of above viewmodel and view remains in the application when it is in the deactivated state.

Can any help me regarding this issue ?


Thanks and regards,
Sivalingam.
Sep 20, 2013 at 6:13 PM
Hi Sivalingam,

If I understood correctly, you want the View and ViewModel to be destroyed after deactivating it from the region. Although the KeepAlive property is needed to free and destroy the Object instance, it also needs the View to don’t have any other strong reference to it.

Basically, when a view / view model that implements the IRegionMemberLifetime interface is deactivated in a Region the RegionMemberLifetimeBehavior will check its KeepAlive property. If this property returns false, the behavior will automatically remove the view / view model from the Region, and as the Region will no longer hold a reference to the view / view model, it will be marked to be garbage collected unless another object of your application is holding a strong reference to it.

Take into account that even if an object is marked to be garbage collected, it will remain in memory until the GC decides to run. What this means is that even if the views / view models are being properly marked to be garbage collected, they would be alive until the garbage collector passes through. You could invoke, however, the GC.Collect() method after deactivating the view / view model in order to detect whether the instances are erased or not (remember that the CG.Collect() method should only be used with testing purposes as its usage is not recommended in a common application).

If your instances are kept alive even after running the GC, it would be useful if you could provide us more information about any reference to the view / view model so we could go through the cause that is keeping them alive in the application.

I hope this helps.

Gabriel Ostrowsky
http://blogs.southworks.net/gostrowsky