Memory leak with Visual Basic code

It appears that when I am running a VB6 script to pull addresses out of a notes Address Book, the notes.exe file leaks with every call to the notes.exe application.

Below is the VB6 code.

Private Sub UserForm_Initialize()

On Error GoTo erh:

Dim m_objNotesSession As Object                 ' Actually a NotesSession

Dim objDB As Object                           ' Actually a NotesDatabase

Dim objView1 As Object                    ' Actually a NotesView

Dim objView2 As Object                     ' Actually a NotesView

Dim objView3 As Object                   ' Actually a NotesView

Dim objDocs1 As Object                            ' Actually a NotesDocument

Dim objDocs2 As Object                        ' Actually a NotesDocument

Dim objDocs3 As Object                      ' Actually a NotesDocument

Dim strAccountID As String

Dim strName As String



On Error GoTo erh:

’ CurrentDocTextBox.Value = ActiveDocument.Name

'set module level variables

Set m_objNotesSession = CreateObject("Notes.Notessession")

m_strServer = ActiveDocument.CustomDocumentProperties("ARTIServer").Value

m_strARTIFilePath = ActiveDocument.CustomDocumentProperties("ARTI").Value



If m_objNotesSession Is Nothing Then

    MsgBox "Failure to activate Notes session. " _

        & m_cstrContactTechSupport, vbOKOnly, m_cstrErrorTitle

    CCNames.Hide

    End

End If



'get ARTI database

Set objDB = m_objNotesSession.GetDatabase(m_strServer, m_strARTIFilePath)

If objDB Is Nothing Then

    If m_strServer = "" Then

        m_strServer = "Local"

    End If

    MsgBox "There was an error locating the ARTI database " _

        & m_strARTIFilePath & " on " & m_strServer _

        & ". " & m_cstrContactTechSupport _

        , vbOKOnly, m_cstrErrorTitle

    CCNames.Hide

    End

End If



' Getting the account the view

Set objView2 = objDB.GETVIEW(m_cstrViewByJobNum)

If objView2 Is Nothing Then

    MsgBox "There was an error locating '" & m_cstrViewByJobNum _

        & "' view in ARTI database " _

        & m_strARTIFilePath & " on '" & m_strServer _

        & "'. " & m_cstrContactTechSupport _

        , vbOKOnly, m_cstrErrorTitle

    CCNames.Hide

    End

End If

' Getting the account the job document

Set objDocs1 = objView2.GetDocumentByKey( _

Left(ActiveDocument.Name, Len(ActiveDocument.Name) - 4))

If objDocs1 Is Nothing Then

    MsgBox "There was an error locating job document " _

        & "associated with open transcription in ARTI database " _

        & m_strARTIFilePath & " on '" & m_strServer _

        & "'. " & m_cstrContactTechSupport _

        , vbOKOnly, m_cstrErrorTitle

    CCNames.Hide

    End

End If

' Getting the account ID

strAccountID = objDocs1.GetItemValue("AccountID")(0)



' Getting the Domino directory associated with this account

Set objView3 = objDB.GETVIEW(m_cstrViewAccount)

If objView3 Is Nothing Then

    MsgBox "There was an error locating '" & m_cstrViewAccount _

        & "' view in ARTI database " _

        & m_strARTIFilePath & " on '" & m_strServer _

        & "'. " & m_cstrContactTechSupport _

        , vbOKOnly, m_cstrErrorTitle

    CCNames.Hide

    End

End If

Set objDocs2 = objView3.GetDocumentByKey(strAccountID & "^^^", True)

If objDocs2 Is Nothing Then

    MsgBox "There was an error locating account '" & strAccountID _

        & "' in ARTI database " & m_strARTIFilePath & " on '" & m_strServer _

        & "'. " & m_cstrContactTechSupport _

        , vbOKOnly, m_cstrErrorTitle

    CCNames.Hide

    End

End If

m_strDirectoryFilePath = Trim(objDocs2.GetItemValue("DirectoryFilePath")(0))

If m_strDirectoryFilePath = "" Then

    MsgBox "There was an error locating" _

        & " the Domino Directory associated with account '" _

        & strAccountID & "'. " & Chr(13) _

        & "Location is not set. " & m_cstrContactTechSupport _

        , vbOKOnly, m_cstrErrorTitle

    CCNames.Hide

    End

End If

Set m_objAddressBook = m_objNotesSession.GetDatabase( _

    m_strServer, m_strDirectoryFilePath)

If m_objAddressBook Is Nothing Then

    MsgBox "There was an error locating" _

        & " the Domino Directory associated with account '" _

        & strAccountID & "'. " & m_cstrContactTechSupport _

        , vbOKOnly, m_cstrErrorTitle

    CCNames.Hide

    End

End If

Set objView1 = m_objAddressBook.GETVIEW(m_cstrViewElysimUsers)

If objView1 Is Nothing Then

    MsgBox "There was an error locating the '" _

        & m_cstrViewElysimUsers & "' view in Domino Directory " _

        & m_strDirectoryFilePath & " associated with account '" _

        & strAccountID & "'. " & m_cstrContactTechSupport _

        , vbOKOnly, m_cstrErrorTitle

    CCNames.Hide

    End

End If

Set objDocs3 = objView1.GetFirstDocument



While Not (objDocs3 Is Nothing)

    strName = objDocs3.GetItemValue("LastName")(0) & ", " & _

        objDocs3.GetItemValue("FirstName")(0)

    If objDocs3.GetItemValue("MiddleInitial")(0) <> "" Then

        strName = strName & " " & _

            objDocs3.GetItemValue("MiddleInitial")(0)

    End If

    strName = strName & " (" & _

        objDocs3.GetItemValue("ExtIDValues")(0) & ")"

    AddressListBox.AddItem strName

    Set objDocs3 = objView1.GetNextDocument(objDocs3)

Wend

Exit Sub

erh:

MsgBox "Error: " & Str(Err.Number) & ": " _

    & Err.Description & " (" & Err.Source & "). " _

    & m_cstrContactTechSupport, vbOKOnly, m_cstrErrorTitle

CCNames.Hide

End

End Sub

Subject: Memory leak with Visual Basic code

Any particular reason why you’re using the old OLE (late binding) calls rather than the COM interface? The COM interface has superceded the OLE (which hasn’t seen any work since the COM interface was introduced). Please note that there’s no reason to go through the Lotus Notes Automation Objects unless you need to manipulate the Notes client UI. The COM interface (Lotus Domino Objects) doesn’t suffer from quite the same problems and is a lot more stable.

Start by adding a reference to the Lotus Domino Objects, and get your session as Lotus.NotesSession. (You can declare the actual types instead of Object.)

Subject: RE: Memory leak with Visual Basic code

Hi Stan,

I am not familiar with the COM interface. I have recently been given this code to see if I can figure out why it is leaking memory. I am fairly new to programming, but I offered to take a look at the code.

Is there anywhere you could point me to learn more about Lotus Domino Objects and the COM interface?

Thanks,

Andrew

Subject: RE: Memory leak with Visual Basic code

Designer help would be a start. Simply search for COM and you should find some introductory documents.

There’s also a Redbook on that topic. Not exactly new, as it was published back in the R5 days (and I don’t think it has been updated since), but most of it should still hold true.

Also Microsoft should have lots of info on COM in general on MSDN. OK, this is a guess on my part, but I’m pretty confident, they have.

Subject: RE: Memory leak with Visual Basic code

With COM you have no access to the NotesUI classes like you did with OLE, but you’re not using them. Apart from that, the basic difference is that you don’t use CreateObject for COM, but you do probably have to tell VB to add the Domino objects to your project so it can set up the binding (it’s been a while since I touched VB, so no I can’t help with details on that). And there’s an initialize() method for NotesSession that you call with COM. Apart from that, there are various differences, mostly minor. Search the Domino Desginer help database for “Accessing the Domino Objects through COM”. That will take you to a page with links to lots of useful info, includign two docs that are very useful, titled “General Exceptions to LotusScript Specifications” and “Specific Exceptions to LotusScript Specifications”.