releasecomobject issue

G

george wen

Hi All,
I wrote a outlook com addin which will popup a dialogue box when user click
on 'replyall' button to confirm whether they do want to reply to all.
However, after using it for a while, we noticed one bug:
If you open a saved .msg file and close it then you won't be able to open it
for a second time.
I did a bit search and found the following discussion:

http://www.devnewsgroups.net/group/microsoft.public.office.developer.outlook.vba/topic61948.aspx

However, the same fix doesn't work for me...

my code for newinspector method:

void inspectors_NewInspector(Outlook.Inspector Inspector)
{
object test = Inspector.CurrentItem;
System.Runtime.InteropServices.Marshal.ReleaseComObject(test);
GC.WaitForPendingFinalizers();

//while (test!=null)
//{
// if
(System.Runtime.InteropServices.Marshal.ReleaseComObject(test) == 0)
// break;
//}

// Check to see if this is a new window we don't already track
OutlookInspector existingWindow =
FindOutlookInspector(Inspector);
if (existingWindow == null)
{
AddInspector(Inspector);
}

}

I even tried to add the garage clean code to the following method:

void WrappedInspectorWindow_Close(object sender, EventArgs e)
{
OutlookInspector window = (OutlookInspector)sender;

object test =
((Outlook.Inspector)window).CurrentItem;//Inspector.CurrentItem;

window.Close -= new EventHandler(WrappedInspectorWindow_Close);
inspectorWindows.Remove(window);

System.Runtime.InteropServices.Marshal.ReleaseComObject(test);
GC.WaitForPendingFinalizers();
while (test != null)
{
if
(System.Runtime.InteropServices.Marshal.ReleaseComObject(test) == 0)
break;
}

}

I am strething my hair out at the moment, if anyone can provide any help it
will be definitely much appreciated!


George Wen
Senior DBA & Application Specialist
 
K

Ken Slovak - [MVP - Outlook]

When you call Marshal.ReleaseComObject() on an object you not only release
the object but its RCW. Only one RCW is instantiated for an object, so by
calling that method on the local Inspector.CurrentItem you're releasing it
for the entire item anywhere in your code. You should only call
ReleaseComObject() when you no longer need that object anywhere in your
code.
 
G

George Wen

Ken said:
When you call Marshal.ReleaseComObject() on an object you not only
release the object but its RCW. Only one RCW is instantiated for an
object, so by calling that method on the local Inspector.CurrentItem
you're releasing it for the entire item anywhere in your code. You
should only call ReleaseComObject() when you no longer need that object
anywhere in your code.
Thank you very much, Ken

According to what you said here, does that mean that the
ReleaseComObject method shouldn't be called with NewInspector event
cause it will release the newly created inspector object as well?

if this is true, shall I place releasecomobject method into the
inspector closing event?

Thanks again for your help, I can send my code to you if you have time
to look at it.

George Wen
 
K

Ken Slovak - [MVP - Outlook]

It means that you have to evaluate when you release your objects if you will
need any other references to them or will need to use those RCW's again. If
you will need them then just set the objects to null, if you won't then call
Marshal.ReleaseComObject().

If in Inspector.Close you release the RCW and then in NewInspector() you
instantiate another RCW for that new Inspector that shouldn't cause
problems.
 
G

George Wen

Ken said:
It means that you have to evaluate when you release your objects if you
will need any other references to them or will need to use those RCW's
again. If you will need them then just set the objects to null, if you
won't then call Marshal.ReleaseComObject().

If in Inspector.Close you release the RCW and then in NewInspector() you
instantiate another RCW for that new Inspector that shouldn't cause
problems.

Ken,thanks a lot for your help.
I am not sure about RCW etc, basically I created the Addin from sample
released by Microsoft and modified the code to suit my need.

I noticed that even if I put the releasecomobject method with
Inspector.Close method, I still can't open the file second time.

I also noticed that while outlook is running and I opened the msg file
and close it again, I can't delete the msg file which means outlook
still has the file handle anyhow.

can I send you my source file and get you to have a look? I am so
desperate at the moment and it just wouldn't work!

Thanks heaps
 
G

George Wen

George Wen wrote:

Hmm...after spending hours on this issue, I eventually came up with a
solution...

the solution is to put the releasecomobject method under
"OutlookInspectorWindow_Close()" method.
I have to call the releasecomobject method for both m_Mail and m_Windows
objects (I am using the 'RulesAddin' project as template...)

Ken, thanks again for your help...
 
G

george wen

hmm...just noticed another issue

while the addin works fine as long as I didn't click on the 'replyall'
button on the opened .msg file, everything seems to be fine.

but if I do click on 'replyall' button and the system will keep the file
handle open and I won't be able to open it for second time!

in my code, I unattach the method by using

((Outlook.ItemEvents_10_Event)m_Mail).ReplyAll -= new
Outlook.ItemEvents_10_ReplyAllEventHandler(m_Mail_ReplyAll);

and my replayall method:

void m_Mail_ReplyAll(object Response, ref bool Cancel)
{
string message = "Did you click on Replay All by mistake?";
//string message = "Are you sure you want to reply to all?";
string caption = "Reply to All";
MessageBoxButtons buttons = MessageBoxButtons.YesNo;
DialogResult result;

result = MessageBox.Show(message, caption, buttons);
if (result == System.Windows.Forms.DialogResult.Yes)
Cancel = true;

Debug.Print("replayall");
}

Do I miss something here?

Hi Ken, can you kindly advise?

Thanks
 
K

Ken Slovak - [MVP - Outlook]

When Outlook loads an item it keeps an internal handle to it for a period of
time, that may be what you're running into. I'm not sure though from your
description what "file" handle you're referring to or what you can't open
again.

If you're using Outlook 2007 you can use the new item.Unload() event to know
when an item is unloaded from the Outlook cache.

I don't do private code reviews except for my consulting clients.
 

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