1

I have a VSTO Outlook 2013 Add-in that has been deployed.

  1. From an end-user's standpoint, is there a way to check if the add-in (COM) has not been deallocated / cleaned-up / closed correctly?
  2. From a developer's standpoint, how do I know what to cleanup? What fields/properties do I know need to look forward, e.g. everything that inherits from IDisposable?
  3. Within ThisAddIn_Shutdown(), is a call simply GC.Collect() suffice?
emily_bma
  • 301
  • 1
  • 15
  • It makes no sense whatsoever to dispose or collect a few milliseconds before your addin terminates. The CLR already does this. Trust the garbage collector. – Hans Passant Jan 09 '15 at 18:26
  • @HansPassant: Why does it make no sense since COM is still involved? Even the official documentation for Office 2013 says so: "continue to implement the OnBeginShutdown and OnDisconnection methods or ThisAddin_Shutdown to release references and allocated memory..." - http://msdn.microsoft.com/en-us/library/bb623945(v=office.15).aspx. – emily_bma Jan 09 '15 at 20:51

2 Answers2

3

In addition to Dmitry's post:

3.Within ThisAddIn_Shutdown(), is a call simply GC.Collect() suffice?

Nope. The way in which .NET objects are stored in memory requires you to run the GC twice. You need to call the Collect method twice with WaitForPendingFinalizers in the following way:

GC.Collect
GC.WaitForPendingFinalizers
GC.Collect
GC.WaitForPendingFinalizers

But I'd recommend releasing Outlook objects instantly. Use System.Runtime.InteropServices.Marshal.ReleaseComObject to release an Outlook object when you have finished using it. Then set a variable to Nothing in Visual Basic (null in C#) to release the reference to the object. Thus, you will avoid a lot of possible issues in Outlook.

You can read more about that in the Systematically Releasing Objects article.

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
  • Not sure why someone down-voted your insightful answer. You are answer is what I have come across in my online research. – emily_bma Jan 09 '15 at 20:52
  • Thank you. This is a good indicator that we should rely on our own opinion and knowledge and don't trust everybody. Also I have noticed that people reject answers coming from non-trusted (i.e. well-known) members. – Eugene Astafiev Jan 10 '15 at 09:30
2
  1. No. Outlook does not know what "correctly" means in your context. If an addin crashes (unhandled access violation etc.), Outlook will try to disable your addin next time unless you explicitly added your addin to the "do not disable" list.

  2. If you have anything worth disposing of (like a database connection, a list of Outlook items, etc.), you need to do that as soon as you are done with that object (Marshal.ReleaseComObject). Otherwise Outlook and .Net run-time do not expect you to do anything special when your addin is unloaded. Keep in mind that Shutdown() callback might not even be called if Outlook initializes fast shutdown, so, again, if you have anything important to release, release it as soon as you are done with it, do not wait for the Shutdown callback.

Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78