Bob Vale
Posts: 25
Joined: 2007-01-29
|
I notice that in all of your examples you use Marshal.ReleaseCOMObject on all of the COM objects you use.
Should I be using these calls in my own code, I was under the impression that the .NET Framework handled all of this for you? If I do need to use it, when do I use it and when shouldn't I use it.
The reason I ask is we are having issues with Outlook 2003 randomly crashing with our addin loaded. This is a full blown crash and restart outlook as opposed to the runtime popping up an error.
|
|
Sergey Grischenko
Add-in Express team
Posts: 7235
Joined: 2004-07-05
|
Hi Bob.
You should use the Marshal.ReleaseCOMObject method in your code.
Please read about it here:
http://www.add-in-express.com/creating-addins-blog/2006/08/17/outlook-not-closing/
Also you can find information about the method in MSDN help.
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.
|
|
Bob Vale
Posts: 25
Joined: 2007-01-29
|
OK I understand the basics of that what I don't understand fully is when the reference count is increased?
Consider the following:
private void adxOlFormsManager1_ADXNewInspector(object inspectorObj) {
Outlook.Inspector oInspector = inspectorObj as Outlook.Inspector;
Should I call release on oInspector (if it isn't null)?
Should I call release on inspectorObj?
If I use the outlookitemevents class and call connectto on a mail item should I release the mail item straight after calling connect to?
If I use the line
Outlook.MailItem mail = oInspector.CurrentItem as Outlook.MailItem;
if the cast fails mail will be null but would the reference counter be increased?
Should I use
object omail=oInspector.CurrentItem;
Outlook.MailItem mail=omail as Outlook.MailItem;
if so do I need to release both omail and mail?
Thanks for all your help. |
|
Sergey Grischenko
Add-in Express team
Posts: 7235
Joined: 2004-07-05
|
Bob, you shouldn't release interfaces passed in the Add-in Express event handlers because they are released in the Add-in Express code.
private void adxOlFormsManager1_ADXNewInspector(object inspectorObj) {
Outlook.Inspector oInspector = inspectorObj as Outlook.Inspector;
Should I call release on oInspector (if it isn't null)?
Should I call release on inspectorObj?
You shouldn't release the 'inspectorObj' and 'oInspector' variables.
If I use the outlookitemevents class and call connectto on a mail item should I release the mail item straight after calling connect to?
It depends on the 'own' parameter of the ConnectTo method. If the parameter is true, you don't need to release the item. Otherwise you need to release the item somewhere in the add-in code.
If I use the line
Outlook.MailItem mail = oInspector.CurrentItem as Outlook.MailItem;
if the cast fails mail will be null but would the reference counter be increased?
The counter will not be increased. I would advise you to use the 'is' operator to make sure that the item is an Outlook mail.
E.g.
object item = oInspector.CurrentItem;
if (item is Outlook.MailItem)
{
}
Marshal.ReleaseComObject(item);
Should I use
object omail=oInspector.CurrentItem;
Outlook.MailItem mail=omail as Outlook.MailItem;
if so do I need to release both omail and mail?
You need to release the 'omail' variable only.
|
|
Bob Vale
Posts: 25
Joined: 2007-01-29
|
Thank you for all your help. |
|