To use Marshal.ReleaseComObject or not to use

J

j

Hi,

I have shared addIn (.net 2.0, c#)

I add custom fields to mailItem object.
Please have a look in a code snippet below:

......
.......
foreach (XmlNode field in fieldList)
{
fieldName = GetFieldName(field);
fieldValue = GetFieldValue(field);
try
{
AddCustomField(mailItem, fieldName, fieldValue);
}
catch (Exception ex)
{ // log }
}

.....
void AddCustomField(Outlook.MailItem mailItem, string fieldName,
string fieldValue)
{
Outlook.UserProperties ups = mailItem.UserProperties;
SetUserProperty(ups, fieldName, typeof(string), fieldValue);//
adds field to mailItem

Marshal.ReleaseComObject(ups);
ups = null;
}


The question is:
I add a few custom fields to mailItem in a loop, do i need to release
ups (Outlook.UserProperties)?
Can this to destroy com object (mailItem) ??

Thanks in advance.
 
K

Ken Slovak - [MVP - Outlook]

Releasing "ups" will not release the mail item, only the UserProperties
object.

If you're doing this in a loop then I would use Marshal.ReleaseComObject()
so you know when the objects are released. If this is being done only a few
times I wouldn't bother since the objects will be garbage collected at some
point in time after the procedure where they are in scope ends.

You always need to balance the perf hit of using Marshal.ReleaseComObject()
and GC.Collect() against the need to know that certain objects have been
released or to prevent the build-up of objects counting against the Exchange
open RPC channel limits.
 
J

j

Releasing "ups" will not release the mail item, only the UserProperties
object.

If you're doing this in a loop then I would use Marshal.ReleaseComObject()
so you know when the objects are released. If this is being done only a few
times I wouldn't bother since the objects will be garbage collected at some
point in time after the procedure where they are in scope ends.

You always need to balance the perf hit of using Marshal.ReleaseComObject()
and GC.Collect() against the need to know that certain objects have been
released or to prevent the build-up of objects counting against the Exchange
open RPC channel limits.















- Show quoted text -


Thanks,

Of cause i understand that releasing "ups" will not relese the
mailItem.
So i wanted to know whether i need to release the "ups"???, btw after
the loop i also releasing the mailItem.

What do you say??
 
K

Ken Slovak - [MVP - Outlook]

If "ups" has not been released yet by the time you try to release the parent
mail item then releasing the parent mail item will fail (not with an error,
but by not releasing that object). So you have to judge that in your
decision.




<snip>
Thanks,

Of cause i understand that releasing "ups" will not relese the
mailItem.
So i wanted to know whether i need to release the "ups"???, btw after
the loop i also releasing the mailItem.

What do you say??
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top