By Adam Nagy
This is something a .NET developer ran into and I thought could be worth sharing. Spoiler alert: the gist of the article is to free objects you do not need anymore. :)
If you have a global variable that is assigned a newly created InteractionEvents (or another object), then it won't be freed until you assign that something else: either a new InteractionEvents object or Nothing. When it gets freed, only then it will try to unsubscribe from the events the given object provides.
In our case we create an InteractionEvents object, start the interaction, finish the interaction, then close the document and open a new one. Now we start a new interaction, at which point our global variable is still pointing at the old InteractionEvents object which by now is completely invalid, since even the document that it was started in is closed now:
When we assign the new InteractionEvents object to oInteraction, only then it will try to unsubscribe from the previous object's events and that's when things go wrong. In theory it should not bring down the system, but in theory we should not hang on to invalid objects either. :)
The "Basic Selection Using Interaction Events API Sample" in the help file is also showing how to use InteractionEvents and is also assigning Nothing to the variables once the interaction is finished:
Public Function Pick(filter As SelectionFilterEnum) As Object ' Initialize flag. bStillSelecting = True ' Create an InteractionEvents object. Set oInteractEvents = ThisApplication.CommandManager.CreateInteractionEvents ' etc... ' Stop the InteractionEvents object. oInteractEvents.Stop ' Clean up. Set oSelectEvents = Nothing Set oInteractEvents = Nothing End Function
Best thing is to follow the same practice in your code: assign Nothing to objects once you don't need them anymore.