Resolving a text extraction exception.

Q:I am using the following VB snippet to extract text from PDF
documents within a Windows Service. Occasionally I get the following

pdftron.Common.PDFNetException: Unknown exception.
  at pdftron.PDF.TextExtractor.Begin(Page page, Rect clip_ptr,
  at ConsoleApplication1.PDFTronExtractor.ExtractText(Page& page,
Rect& pos)

This app simulates what we actually do - A windows service that
monitors a queue, when an item enters the queue it retrieves the
appropriate file, and extracts the text from it.

Imports System.Drawing

''' <summary>
''' Extracts text from PDF files.
''' </summary>
Public Class PDFTronExtractor

#Region "Local Members"
    Private mstrFilePath As String
    Private mstrAddressLines() As String
    Private mRect As Rectangle
#End Region

#Region "Properties"
    ''' <summary>
    ''' Returns the address lines extracted
    ''' </summary>
    Public ReadOnly Property AddressLines() As String()
            Return mstrAddressLines
        End Get
    End Property

    ''' <summary>
    ''' The path of the PDF files.
    ''' </summary>
    Public Property FilePath() As String
            Return mstrFilePath
        End Get
        Set(ByVal value As String)
            mstrFilePath = value
        End Set
    End Property

    Public Property Rectangle() As Rectangle
            Return mRect
        End Get
        Set(ByVal value As Rectangle)
            mRect = value
        End Set
    End Property

    Public Sub Dispose()
    End Sub

#End Region

#Region "Functions"

    Public Sub New()
    End Sub

    ''' <summary>
    ''' Extracts the text from the document.
    ''' </summary>
    ''' <remarks></remarks>
    Public Function Extract() As Boolean
        mstrAddressLines = Nothing

        ' Open the test file
        Dim doc As PDFDoc = New PDFDoc(mstrFilePath)

        Dim page As Page = doc.GetPage(1)

        Dim r As Rect
        With mRect
            Dim x1, x2, y1, y2 As Double
            x1 = .X
            x2 = .X + .Width
            y1 = page.GetPageHeight - (.Y + .Height)
            y2 = y1 + .Height
            r = New Rect(x1, y1, x2, y2)
        End With

        mstrAddressLines = ExtractText(page, r).Split(New String()
{vbNewLine}, StringSplitOptions.RemoveEmptyEntries)

        Extract = True
    End Function

    Private Function ExtractText(ByRef page As Page, ByRef pos As
Rect) As String
        Dim sb As New System.Text.StringBuilder()
        Dim te As New PDFTRON.PDF.TextExtractor()
        Dim bNewLine As Boolean
        te.Begin(page, pos,

        Dim w As PDFTRON.PDF.TextExtractor.Word
        Dim line As PDFTRON.PDF.TextExtractor.Line = te.GetFirstLine
        While line.IsValid
            bNewLine = True
            w = line.GetFirstWord
            While w.IsValid
                If Not bNewLine Then
                    sb.Append(" ")
                End If
                bNewLine = False
                w = w.GetNextWord
            End While
            line = line.GetNextLine
        End While

        te.Dispose() <---- ADD this line

        Return sb.ToString
    End Function

#End Region
End Class

Module Module1
    Sub Main()
        Const FilePath As String = "OpenOffice PDF.pdf"
        Dim ext As New PDFTronExtractor
                ext.FilePath = FilePath
        Catch ex As Exception
        End Try
    End Sub
End Module
A: The problem is that TextExtractors’s Dispose() method is not called
(te.Dispose() in ExtractText). Not calling Dispose() on TextExtractor/
ElementBuilder/ElementWriter/PDFView can lead to resource exhaustion.
You may want to use .NET IDispose pattern or 'use' keyword...