Reputation: 1047
I'm wondering how to dispose a Page correctly in WinRT.
In my page_loaded event I hook several events like:
_navigationHelper.LoadState += navigationHelper_LoadState;
_navigationHelper.SaveState += navigationHelper_SaveState;
_button.Click += button_click;
As I don't have an instance from my page I can not dispose it. Am I responsible to unhook such events?
Is it right that if the destructor is called everything is disposed and the GC takes care of all references?
Many thanks Dani
Upvotes: 1
Views: 1554
Reputation: 31724
If your page doesn't hold references to large resources - it might be fine to just let it be garbage collected. Otherwise you can consider releasing those on the Unloaded
event or
OnNavigatedFrom()
override. The page itself doesn't implement IDisposable
, so you can't dispose it and it's likely not what you'd want since a control like Page
is not a heavy resource.
Overall - garbage collection takes care of all objects that lose a path to GC root. You just need to make sure you don't cause a leak by leaving such a connection hanging (I'd say most typically by not removing static event handlers). To release larger resources you should implement the IDisposable
interface correctly (look up "IDisposable pattern").
Upvotes: 2