Releasing COM objects in Outlook Add-ins

Add-in Express™ Support Service
That's what is more important than anything else

Releasing COM objects in Outlook Add-ins
What are the rules? 
Mike M.




Posts: 31
Joined: 2006-11-13
In a recent topic from the 29th of January 2007, there was a disussion about when it is necessary to release COM objects in ADX based add-ins. From this discussion I see the following rules as to when one should release COM objects:

1) You shouldn???t release Com objects passed into the Add-in Express event handlers as parameters.
2) Everytime I access a COM object which is not passed to me be ADX code, for instance the Selection of an Explorer, then I must release this object?
3) Is the Selection COM object on the Explorer is not created until I access it (I don???t know much about COM programming)?

Question 1 of 4:
Is this correct and am I missing any rules with respect to releasing COM objects?

Question 2 of 4:
Just to be sure: I don???t need to call Marshal.ReleaseCOMObject on the explorer object in this event handler right?:

adxOutlookEvents.ExplorerSelectionChange += new ADXOlExplorer_EventHandler(adxOutlookEvents_ExplorerSelectionChange);

private void adxOutlookEvents_ExplorerSelectionChange(object sender, object explorerObject)
{
Explorer explorer = (Explorer) explorerObject;
....
// no need to call release on explorer here ?
}

Question 3 of 4:
In the ADX documentation of ADX Extensions for Outlook, there is this sample code on page 17:

C#
private void ADXOlForm1_ADXSelectionChange()
{
Outlook._Application OlApp = this.OutlookAppObj as Outlook._Application;
Outlook.Selection Selection = OlApp.ActiveExplorer().Selection;
if (Selection.Count != 0)
{
Outlook.MailItem item = Selection[1] as Outlook.MailItem;
SentLabel.Text = "Sent: " + item.SentOn;
}
}

This example is missing a release isn???t it? It would be correct to release the Selection object like this right?:

private void ADXOlForm1_ADXSelectionChange()
{
Try
{
Outlook._Application OlApp = this.OutlookAppObj as Outlook._Application;
Outlook.Selection Selection = OlApp.ActiveExplorer().Selection;
if (Selection.Count != 0)
{
Outlook.MailItem item = Selection[1] as Outlook.MailItem;
SentLabel.Text = "Sent: " + item.SentOn;
}
Finally
{
Marshal.ReleaseCOMObject(Selection);
}
}


Question 4 of 4:
Is there any way one can detect, runtime and/or compile time whether there are dangling COM objects which have not been released? Are there anygood debugging techniques for detecting and handling misssing releases of COM objects?
Posted 06 Feb, 2007 05:39:50 Top
Sergey Grischenko


Add-in Express team


Posts: 7233
Joined: 2004-07-05
Hi Mike.

1. Correct.
2. Correct.
3. No, the Selection object on the Explorer is always created. But when you get a reference to the object, the reference counter is increased by one. Your code of the ADXSelectionChange event handler is correct but I would release the ActiveExplorer object as well.
4. To detect the design time and runtime in the add-ins, you can use the DesignMode property of the addinmodule. There is no reliable way to determine whether you need to call the ReleaseComObject method or not.


P.S. Note that we take up your forum requests in the order we receive them.
Besides, it may take us some time to investigate your issue. Please be sure we will let you know as soon as the best possible solution is found.
Posted 06 Feb, 2007 09:05:48 Top
Fedor Shihantsov


Guest


Hi Mike,

>Question 1 of 4:
Yes, it is correct.

>Question 2 of 4:
Yes, you should not call Marshal.ReleaseCOMObject here.

>Question 3 of 4:
Yes, you are right.
This code contains several mistakes.
The code should be like this

private void ADXOlForm1_ADXSelectionChange()
{
    textBox1.Text = string.Empty;
    Outlook._Application OlApp = this.OutlookAppObj as Outlook._Application;

    Outlook.Explorer Explorer = OlApp.ActiveExplorer();
    if (Explorer != null)
    {
        Outlook.Selection Selection = Explorer.Selection;
        if (Selection != null)
        {
            if (Selection.Count != 0)
            {
                Outlook.MailItem item = Selection.Item(1) as Outlook.MailItem;
                if (item != null)
                {
                    textBox1.Text = "Sent: " + item.SentOn;
                    Marshal.ReleaseComObject(item);
                }
            }
            Marshal.ReleaseComObject(Selection);
        }
        Marshal.ReleaseComObject(Explorer);
    }
}


Please pay attention to the conversion of the OlApp.ActiveExplorer().Selection statement.

>Question 4 of 4:
Sorry, I don't know such a way.

P.S. Note that we take up your forum requests in the order we receive them.
Besides, it may take us some time to investigate your issue. Please be sure we will let you know as soon as the best possible solution is found.
Posted 06 Feb, 2007 09:29:16 Top