none
VSTO 2008 2010 COM object handling RRS feed

  • Question

  • I have a subroutine that is called in various places in a VSTO 2008 and VSTO 2010 project.  I would like to know whether the following code warrants disposing of COM objects.  I realize this has been tackled in numerous threads, however, I have not come back with a clear understanding on the proper method.

    Sub Update_Inspectors()
    If Globals.ThisAddIn.Application.Inspectors.Count = 0 Then Exit Sub
    Dim insps As Outlook.Inspectors
    Try
    	insps = Globals.ThisAddIn.Application.Inspectors	
    	Dim ribbonCollection As ThisRibbonCollection = Nothing
    	Dim insp As Outlook.Inspector = Nothing
    	For i As Integer = 1 To insps.count
    		insp = insps(i)
    		Dim item As Outlook.MailItem = TryCast(insp.CurrentItem, Outlook.MailItem)
    		If item Is Nothing Then
    		ElseIf item.Sent = False Then
    			ribbonCollection = Globals.Ribbons(insp)
    			ribbonCollection.myRibbon.ToggleButton1.Checked = True
    			Menu1_Populate(ribbonCollection.myRibbon.Menu1)
    		End If
    		item = Nothing
    	Next
    	insp = Nothing
    	ribbonCollection = Nothing
    Catch ex As Exception
    ' handle error
    End Try
    insps = Nothing
    End Sub

    Thank you
    Wednesday, March 7, 2012 6:13 PM

Answers

  • Declare the MailItem object outside of the For loop. Otherwise you're creating a new object in each loop pass. If you declare the object outside of the loop you only create one instance of the object.

    --
    Ken Slovak
    [MVP - Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    Reminder Manager, Extended Reminders, Attachment Options
    http://www.slovaktech.com/products.htm
     
     
    "J Wagner" <=?utf-8?B?SiBXYWduZXI=?=> wrote in message news:65c98479-bbf2-4975-8f74-714d6aa1b795...
    The Addin calls the subroutine when the user changes a setting in the application.  The subroutine then loops through all open MailItems and applies the requested changes to each inspector ribbon.  Other than the ribbon change, the message should stay intact, until they send or close it.  I just want to make sure objects are handled efficiently within the sub.

    Ken Slovak MVP - Outlook
    • Marked as answer by J Wagner Thursday, March 8, 2012 6:38 PM
    Thursday, March 8, 2012 3:37 PM
  • From that perspective your code looks ok, i would just add this releasecomobject call after done with casted object.
    • Marked as answer by J Wagner Thursday, March 8, 2012 6:38 PM
    Thursday, March 8, 2012 9:14 AM

All replies

  • not sure if i understand your question as it is not clear from sample what you are concerned about but i would call Marshal.ReleaseComObject on each succesfull cast to MailItem (of course after you are done with it) because casting on RCW is implemented as QueryInterface - succesful one will increase internal ref count on object.
    Wednesday, March 7, 2012 8:24 PM
  • The Addin calls the subroutine when the user changes a setting in the application.  The subroutine then loops through all open MailItems and applies the requested changes to each inspector ribbon.  Other than the ribbon change, the message should stay intact, until they send or close it.  I just want to make sure objects are handled efficiently within the sub.
    Wednesday, March 7, 2012 9:30 PM
  • From that perspective your code looks ok, i would just add this releasecomobject call after done with casted object.
    • Marked as answer by J Wagner Thursday, March 8, 2012 6:38 PM
    Thursday, March 8, 2012 9:14 AM
  • Declare the MailItem object outside of the For loop. Otherwise you're creating a new object in each loop pass. If you declare the object outside of the loop you only create one instance of the object.

    --
    Ken Slovak
    [MVP - Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    Reminder Manager, Extended Reminders, Attachment Options
    http://www.slovaktech.com/products.htm
     
     
    "J Wagner" <=?utf-8?B?SiBXYWduZXI=?=> wrote in message news:65c98479-bbf2-4975-8f74-714d6aa1b795...
    The Addin calls the subroutine when the user changes a setting in the application.  The subroutine then loops through all open MailItems and applies the requested changes to each inspector ribbon.  Other than the ribbon change, the message should stay intact, until they send or close it.  I just want to make sure objects are handled efficiently within the sub.

    Ken Slovak MVP - Outlook
    • Marked as answer by J Wagner Thursday, March 8, 2012 6:38 PM
    Thursday, March 8, 2012 3:37 PM