title: Do you have to call gc.collect explicitly sometimes with COM objects
body: <p>Here is the function:</p>
  
  <pre><code>        Private Sub LoadEmail()
  
          Dim loSession As RDOSession = Nothing
          Dim loMessages As RDOItems = Nothing
  
          Try
              moNDRs = New List(Of NonDeliveryRecord)
              loSession = New Redemption.RDOSession
              loSession.LogonExchangeMailbox(MailAccountName, MailServerName)
              loMessages = loSession.GetDefaultFolder(rdoDefaultFolders.olFolderInbox).Items
              Dim Counter = 0
              For Each loMessage As RDOMail In loMessages
                  Counter += 1
                  moNDRs.Add(CreateNDRRecord(loMessage))
                  Marshal.ReleaseComObject(loMessage)
                  loMessage = Nothing
                  If Counter Mod 100 = 0 Then GC.Collect()
              Next
  
  
          Finally
              If loSession IsNot Nothing Then
                  loSession.Logoff()
                  Marshal.FinalReleaseComObject(loSession)
                  loSession = Nothing
              End If
  
              If loMessages IsNot Nothing Then
                  Marshal.FinalReleaseComObject(loMessages)
                  loMessages = Nothing
              End If
  
          End Try
  
      End Sub
  </code></pre>
  
  <p>The Message classes uses above are <a href="http://www.dimastr.com/redemption/home.htm" rel="nofollow">Redemption</a>. If you look in the function above you will see: </p>
  
  <pre><code>If Counter Mod 100 = 0 Then GC.Collect()
  </code></pre>
  
  <p>Which is what i had to do to fix the problem we are having. I've been playing around with memory profilers this morning (ants &amp; dottrace) to see if i could figure anything out but so far everything looks fine. I'm no low level won't who knows the ins and outs of windgb. </p>
  
  <p>The error i'm getting is:
  Error in IMAPISession::OpenEntry: MAPI_E_TOO_BIG</p>
  
  <p>The line where i always get the error is commented in the code below. I always get the error after ~450 iterations.</p>
  
  <p>Is this one of the few times you have to use gc.collect is when your dealing with COM objects? </p>
  
  <p>Here is the CreateNDR function with the line the error happens on:</p>
  
  <pre><code>        Public Function CreateNDRRecord(ByVal voMessage As RDOMail) As NonDeliveryRecord
  
          Dim loItem As RDOReportItem = Nothing
          Dim loMatches As MatchCollection = Nothing
          Dim loNonDeliveryCode As NonDeliveryRecord = New NonDeliveryRecord
          Dim lsMessage As String = String.Empty
  
  
          Try
              loNonDeliveryCode.IsBadMessage = False
              loNonDeliveryCode.MailMessageId = voMessage.EntryID
  
              'Debug.Print(voMessage.MessageClass.Equals("REPORT.IPM.Note.NDR").ToString())
              If voMessage.MessageClass.Equals("REPORT.IPM.Note.NDR") Then 'error always happens here
                  loItem = CType(voMessage, RDOReportItem)
                  If voMessage.Recipients.Count &lt;&gt; 0 Then
                      loNonDeliveryCode.EmailAddress = voMessage.Recipients(1).Name
                  End If
                  loNonDeliveryCode.IsUndeliverable = True
                  lsMessage = loItem.ReportText
  
              ElseIf voMessage.Subject.Contains(mconSeparator) Then
                  loNonDeliveryCode.EmailAddress = voMessage.Subject.Substring(voMessage.Subject.LastIndexOf(mconSeparator) + mconSeparator.Length)
                  loNonDeliveryCode.ErrorCode = String.Empty
                  loNonDeliveryCode.IsUndeliverable = True
                  lsMessage = voMessage.Body
              End If
  
              If loNonDeliveryCode.IsUndeliverable Then
  
                  loMatches = GetErrorType(lsMessage)
  
                  If loMatches.Count &gt; 0 Then
                      loNonDeliveryCode.ErrorCode = loMatches(loMatches.Count - 1).Value
                  End If
  
                  Dim loNDRId = GetErrorCode(loNonDeliveryCode.ErrorCode)
  
                  If loNDRId.Count &gt; 0 Then
                      loNonDeliveryCode.ErrorCodeId = CType(CType(loNDRId(0), DataRow).Item("NonDeliveryCodeId"), Integer)
                      loNonDeliveryCode.ErrorDescription = CType(CType(loNDRId(0), DataRow).Item("Description"), String)
                      loNonDeliveryCode.MarkAsInvalid = CType(CType(loNDRId(0), DataRow).Item("MarkAsInvalid"), Boolean)
                  Else
                      If voMessage.MessageClass.Equals("REPORT.IPM.Note.NDR") Then
                          loNonDeliveryCode.ErrorCode = String.Empty
                          loNDRId = GetErrorCode(loNonDeliveryCode.ErrorCode)
                          loNonDeliveryCode.ErrorCodeId = CType(CType(loNDRId(0), DataRow).Item("NonDeliveryCodeId"), Integer)
                          loNonDeliveryCode.ErrorDescription = CType(CType(loNDRId(0), DataRow).Item("Description"), String)
                          loNonDeliveryCode.MarkAsInvalid = CType(CType(loNDRId(0), DataRow).Item("MarkAsInvalid"), Boolean)
                      Else
                          loNonDeliveryCode.ErrorCode = String.Empty
                          loNonDeliveryCode.ErrorCodeId = 1
                      End If
                  End If
  
              End If
  
  
              Return loNonDeliveryCode
  
          Catch Ex As Exception
              loNonDeliveryCode.IsUndeliverable = False
              loNonDeliveryCode.IsBadMessage = True
              Return loNonDeliveryCode
  
          Finally
              If loItem IsNot Nothing Then
                  Marshal.FinalReleaseComObject(loItem)
                  loItem = Nothing
              End If
  
              If voMessage IsNot Nothing Then Marshal.ReleaseComObject(voMessage)
  
              If loMatches IsNot Nothing Then
                  loMatches = Nothing
              End If
  
          End Try
  </code></pre>