Why are agents getting frequent 'Out of Memory' errors?
Add-ins, Agent Console > Out of Memory errors when add-ins are present
We are getting Out of Memory errors randomly and have add-ins included in our desktop configuration. The code in the add-ins contains IGlobalContext to obtain an IAutomationcontext reference. Using IGlobalContext.IAutomationContext to subscribe and unsubscribe to events can cause unsubscribe events to not be handled by the framework. This is the correct flow, however there is a slight caveat. Everytime IGlobalContext.IAutomationContext is used a new AddInSideAdapter will be created to reference the AutomationContext. In practice, you wire up the event like this:
IGlobalContext.AutomationContext.CurrentEditorTabChanged += eventHandler;
IGlobalContext.AutomationContext.CurrentEditorTabChanged -= eventHandler;
In this example, you are not actually unwiring the event in the second line that you wired up in the first line. IGlobalContext.AutomationContext is not static nor a singleton.
The proper way to do this would be to set your AutomationContext to a local variable to ensure that you are always referencing the same object. For example:
private IAutomationContext intitialAutomationContext;
intitialAutomationContext = globalContext.AutomationContext;
intitialAutomationContext .CurrentEditorTabChanged += eventHandler;
intitialAutomationContext .CurrentEditorTabChanged -= eventHandler;
In this case the event will be unsubscribed properly.
Everytime IGlobalContext.IAutomationContext is used a new AddInSideAdapter is created to reference the AutomationContext. IGlobalContext.AutomationContext is not static nor a singleton.