What is the correct way to cleanup/dispose resources used by PDFNet for Windows Store Apps?

Q: I have a Page where the user selects files, and another where the user views the Selected PDF. When the user navigates back from the PDF viewing page to the file page, I want to clean up resources used by PDFNet. What is the correct order of cleaning up the Tools, the PDFViewCtrl, and the PDFDoc.

This is good to put in OnNavigatedFrom for whatever page uses the PDFViewCtrl.

Also, the PDFViewCtrl cannot be disposed manually, instead, we’ve added a PDFViewCtrl.FreeResources that removes all the underlying structures, which means less memory is used until the garbage collector kicks in.

I like to add the following:

PDFDoc doc = this.PDFViewCtrl.GetDoc();

this.PDFViewCtrl.CloseDoc();

if (doc != null)
{
await CloseDocAsync(doc);
}

this.PDFViewCtrl.FreeResources();

Where CloseDocAsync just calls doc.Dispose in a background thread (in case it’s a large, complicated document). This might not be necessary, as all document I know of dispose rather quickly. But it doesn’t hurt. Even 200 ms of lag on the UI thread might be disruptive.

One thing to keep in mind is that in order for the PDFViewCtrl to be re-claimed by the garbage collector, all events registered to it need to be unregistered. That’s why the ToolManager.Dispose() should be called when you navigate away from a page as well.
In that case, just call ToolManager.Dispose before any of the code I added above.
A: You need to call PDFViewCtrl.CloseDoc() first, and then you can call PDFDoc.Dispose().