How do I deal with a random error when using PDFViewCtrl to view PDF in my app?

Q: We encounter apparently random “UNKNOWN ERROR” when opening PDF in
PDFViewCtrl

Sometime, I open only 3 or 4 documents before the app crash, and
sometime I can open 40 doc before crash.

This is the code I use:

currentObjDoc = New PDFDoc(FilePath)

If (currentObjDoc IsNot Nothing) Then
       If currentObjDoc.InitSecurityHandler Then
       Me.GetDoc.Close()
       currentObjDoc.Lock()
            Me.SetDoc(currentObjDoc)
            currentObjDoc.Unlock()
            Me.Show()
       End If
End If

“Me” is PDFViewCtrl inerits object.

How can prevent this crash?
--------------------

A: You need to get a reference to the old document, then set a new
document, then close the old document:

Dim olddoc As PDFDoc = _pdfview.GetDoc()
_pdfdoc = New PDFDoc(filename)
_pdfview.SetDoc(_pdfdoc)
If Not (olddoc Is Nothing) Then
  olddoc.Dispose()
End If

For a concrete sample, please see PDFViewSimple sample project (http://
www.pdftron.com/pdfnet/samplecode.html#PDFViewSimple).

Btw. in this case there is no need to Lock()/Unlock() . This is needed
only is you are reading or modifying data in the document. In your
case it seems that you are only viewing and closing the document.