View Single Post
  #4  
Old December 18th 08, 03:12 PM posted to microsoft.public.outlook.program_addins
Ken Slovak - [MVP - Outlook]
external usenet poster
 
Posts: 5,848
Default OpenSharedItem and .MSG files

Let's say I have an object oMail. When finished with it I'd use code like
this, assuming that I have no need for any other references to that object
in any context at all:

if (oMail != null)
{
Marshal.ReleaseComObject(oMail);
oMail = null;
}

GC.Collect();
GC.WaitForPendingFinalizers();

That call to ReleaseComObject releases the RCW for that oMail object.

The down side to that is if you have live references to the underlying mail
item represented by oMail anywhere else in your program.

Those will become invalid when the RCW is released since the Interop only
creates 1 RCW no matter how many object references to the object you
instantiate in different scopes.

So when passing a reference to oMail by value to a procedure if the
procedure releases the RCW the passed reference will be invalid in the
calling procedure.

--
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


"SeekerOfTruths" wrote in message
...
Sorry, I should have said what it was written in to begin with - yes, it's
.Net (C#) code. Unfortunately .Net and C# are kind of new tech to me (I'm
more of a C++ Extended MAPI store provider kind of person) so I'm just
sort
of writing the code and picking things up as I go along (very bad practice
I
know, but needs must when the devil drives).

I did try throwing in a ReleaseComObject call to see if it had any effect
but didn't notice any change in behaviour, but then I hadn't included any
garbage collection calls.


Ads