none
Releasing COM objects RRS feed

  • Question

  • I pretty much have the rules down for calling Marshal.ReleaseComOject(...), but there's a case that I'm not sure about.

    Which is cleaner?

    void InspectorActivate( .... object inspector )
    
    {
    
         Outlook._Inspector insp = null;
         Outlook._MailItem mailItem = null;
         object oMailItem = null;
    
         
         insp = inspector As Outlook._Inspector;
    
    
         // Does this If condition create an RCW that will not
         // be released?        
    
         if( insp.CurrentItem is Outlook._MailItem )
         {
             mailItem = insp.CurrentItem as Outlook._MailItem;
         }
    
    
         ///  I'm sure this code is ok
        oMailItem = insp.CurrentItem;
         if( oMailItem is Outlook._MailItem )
         {
             mailItem = oMailItem As Outlook._MailItem;
         }
    
        // Code for Releasing COM objects
    
    }



    • Edited by GermanEZI Monday, August 21, 2017 4:56 PM typo
    Monday, August 21, 2017 4:55 PM

Answers

  • "is' operator will retrieve a COM object that you cannot explicitly release. Do not "is". Use "as" (it does not raise an exception) and release the returned object.

    It would not matter much in your particular case as it is pretty hard to open a few hundred items before the GC kicks in. But it is important if you are processing a large number of items in a loop.

    Change the code to

    mailItem = insp.CurrentItem as Outlook._MailItem;
    if( mailItem != null)
    {
        //do something
        Marshal.ReleaseComObject(mailItem);
    }
    
    ...
    
    object oMailItem = insp.CurrentItem;
    MailItem mailItem = oMailItem As Outlook._MailItem
    if( mailItem != null)
    {
        Marshal.ReleaseComObject(mailItem);
    }
    Marshal.ReleaseComObject(oMailItem);
    
    


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    • Marked as answer by GermanEZI Monday, August 21, 2017 5:38 PM
    Monday, August 21, 2017 5:10 PM

All replies

  • "is' operator will retrieve a COM object that you cannot explicitly release. Do not "is". Use "as" (it does not raise an exception) and release the returned object.

    It would not matter much in your particular case as it is pretty hard to open a few hundred items before the GC kicks in. But it is important if you are processing a large number of items in a loop.

    Change the code to

    mailItem = insp.CurrentItem as Outlook._MailItem;
    if( mailItem != null)
    {
        //do something
        Marshal.ReleaseComObject(mailItem);
    }
    
    ...
    
    object oMailItem = insp.CurrentItem;
    MailItem mailItem = oMailItem As Outlook._MailItem
    if( mailItem != null)
    {
        Marshal.ReleaseComObject(mailItem);
    }
    Marshal.ReleaseComObject(oMailItem);
    
    


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    • Marked as answer by GermanEZI Monday, August 21, 2017 5:38 PM
    Monday, August 21, 2017 5:10 PM
  • Thanks!  
    Monday, August 21, 2017 5:38 PM