即使在我尽了最大的努力之后,我仍然泄露互操作引用,这一点在Outlook中是显而易见的,而不是单独关闭的。我已经把我的问题降到了尽可能小的样本上,并确定了故障线,,你能发现我缺少什么吗?
如果对象不是null,FinalReleaseComObjectSafe将接受对象并在其上调用FinalReleaseComObjectSafe。
Outlook.Application app = null;
Outlook.NameSpace ns = null;
Outlook.MAPIFolder explorerFolder = null;
Outlook.Explorers explorers = null;
Outlook.Explorer explorer = null;
try
{
app = new Outlook.Application();
ns = app.Session;
explorerFolder = ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts);
explorers = app.Explorers;
explorer = explorers.Add(explorerFolder); // If I omit this line Outlook closes by itself
}
finally
{
CSharpUtilities.FinalReleaseComObjectSafe(explorer);
CSharpUtilities.FinalReleaseComObjectSafe(explorers);
CSharpUtilities.FinalReleaseComObjectSafe(explorerFolder);
CSharpUtilities.FinalReleaseComObjectSafe(ns);
CSharpUtilities.FinalReleaseComObjectSafe(app);
GC.Collect();
GC.WaitForPendingFinalizers();
}发布于 2015-05-13 09:41:47
使用应用程序类的退出方法关闭应用程序。在Outlook中有外接程序运行吗?
首先,我建议使用ReleaseComObject方法。如果其他人继续使用引用/发布的对象,FinalReleaseComObject可能会引入新的bug。没有必要使用GC的方法强制刷新堆。系统释放对象的文章陈述如下:
使用System.Runtime.InteropServices.Marshal.ReleaseComObject在完成使用后释放Outlook对象。然后在Visual中将变量设置为Nothing (C#中为null),以释放对对象的引用。
代码在何时何地运行?
https://stackoverflow.com/questions/30209430
复制相似问题