Partager via


An Inspector can be leaked if you call GetInspector() in the Application_ItemSend() event.

 

The Problem

There is a case in Outlook programming where an Inspector object may be created and not released.  The condition is when the Application_ItemSend() event is fired, and in that event the GetInspector() method is called when a Meeting Request is being sent.  In this case, a new Inspector is created, and is not released. Also, the CurrentItem property on the inspector will be invalid and Outlook will return an error if you try to access it.  Here is a VBA code example that can trigger the behavior:

Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)     Debug.Print "Step 1: ", Application.Inspectors.Count     If MsgBox("Get Inspector?", vbYesNo) = vbYes Then         Set oInsp = Item.GetInspector         Set oInsp = Nothing     End If     Debug.Print "Step 2: ", Application.Inspectors.Count End Sub Sub GetInspectorsCount()     Debug.Print "Inspectors:", Application.Inspectors.Count End Sub

 

Workarounds

In this case there are a couple of workarounds. 

  • Attempt to call the Close() method on the Inspector that is returned. 

  • Attempt to move the code to the AppointmentItem_Send event for this case.

The second workaround above means that depending on what your code is doing, it may need to be changed to not listen on the Application_ItemSend event, and instead listen to the item-level Item_Send event for each item.