VSTO Outlook Add-ins – Reference Leaks

Not too long ago I came across an annoying issue with my Outlook Add-in which creates a form region at the bottom of the contact and distribution list inspector windows. Apparently, if you edit certain fields (like check-boxes) when a VSTO add-in is loaded the reference count is not properly decremented which leaves the OutlookItem in an edited state.

To fix the issue you have to edit some auto-generated VSTO code and release the OutlookItem during a setup event like so:

private void RegionFactory_FormRegionInitializing(object sender, Outlook.FormRegionInitializingEventArgs e)
{
    Marshal.ReleaseComObject(e.OutlookItem);
}

This block of code can be found in the region block labeled: #region Form Region Factory

Apparently, a Microsoft support engineer posted about this back in August of 2008, but I never came across it until after working with a different very nice MSDN support engineer.

The other day however, I realized that the trick above does not in fact work for Distribution List (IPM.DistList) form regions. Watch the following video for proof!

I contacted the very nice engineer who helped me before to re-open/create a support ticket about the issue. While waiting for a response however, I kept trying to fix the issue and finally did. I’m quite hopeful that my fix is not the best answer, as it’s truly a bizarre hack.

Take a look at the Designer code generated for your region. The IFormRegionFactory which creates an instance of the actual ImportedFormRegion does not need to return anything for the region to load. In fact, if you throw an exception from IFormRegionFactory.CreateFormRegion, the DistListItem is properly unreferenced when the inspector is closed.

You can watch the following video to see it in action!

This entry was written by MET , posted on Friday January 01 2010at 11:01 am , filed under programming and tagged , , . Bookmark the permalink . Post a comment below or leave a trackback: Trackback URL.

Comments are closed.