Removing Views & Garbage Collection

Jun 11, 2009 at 11:05 PM

Can anyone help?  I can't seem to figure out why my views and view models are not being garbage collected when I'm removing a view from a region.  Could the regionmanager be holding the objects in scope?  Pretty sure its not the container (unity).  I've tried to figure out from the quick starts and reference implementation when various views and presentation models are garbage collected however it looks like they don't get GC'd when removing the view from the region.

Any help greatly appreciated.

 

Jun 14, 2009 at 11:02 AM

I think I've tracked down the source of the problem but it still doesn't make any sense.  Basically, I add a view to a tab control region and pass in the viewmodel as the datacontext.  When I remove the view from the region it gets GC'd as expected however the viewmodel doesn't.  I've stripped out everything from the view and viewmodel so they are basically empty classes/user controls.  The interesting thing is that if I pass in the viewmodel to the view but don't set it to the datacontext, the view and viewmodel get GC'd when the view is removed from the region.  So, it looks like something is holding a reference to the datacontext (even when it is GC'd!!) or the content of the datacontext.  I thought it might be the region adapter causing all the trouble but that doesn't make sense either.

Any thoughts?