Professional OPC
Development Tools


Memory leak: EasyDAClient stays live after Dispose() and set it to null.

20 Feb 2015 11:57 - 22 Feb 2015 19:56 #2825 by Emilio
I have the following problem:

A standard application with a GUI is running.
When the user switches to a page, an OPC DA Subscription with many OPC Items starts.
When the user switches back to another page, the OPC DA Subscription is cleared and EasyDAClient disposed and nulled.
These switches can occur many times, it is a user choice.

I observed that there is some memory leak each time the user ends the OPC subscription.

Here below a simplified working sample code, after that the result after analysis with RED GATE ANTS Memory Profiler.

The MonitoredItems class creates the arguments for the subscription, nothing special.

internal class TestOpcContainer
        private EasyDAClient opcClient = null;
        private int[] handles = null;
        private MonitoredItems monitoredItems = null;
        public void Start()
            if (this.opcClient == null)
                this.opcClient = new EasyDAClient();
                this.monitoredItems = new MonitoredItems(1000);
                this.monitoredItems.Add(0, "Softing.OPCToolboxDemo_ServerDA.1", "increment.I8");
                this.monitoredItems.Add(1, "Softing.OPCToolboxDemo_ServerDA.1", "increment.UI8");
                this.monitoredItems.Add(2, "Softing.OPCToolboxDemo_ServerDA.1", "increment.R4");
                this.monitoredItems.Add(3, "Softing.OPCToolboxDemo_ServerDA.1", "increment.CY");
                this.monitoredItems.Add(4, "Softing.OPCToolboxDemo_ServerDA.1", "increment.R8");
                this.monitoredItems.Add(5, "Softing.OPCToolboxDemo_ServerDA.1", "increment.BOOL");
                this.monitoredItems.Add(6, "Softing.OPCToolboxDemo_ServerDA.1", "increment.I1");
                this.monitoredItems.Add(7, "Softing.OPCToolboxDemo_ServerDA.1", "increment.UI1");
                this.monitoredItems.Add(8, "Softing.OPCToolboxDemo_ServerDA.1", "increment.INT");
                this.monitoredItems.Add(9, "Softing.OPCToolboxDemo_ServerDA.1", "increment.UINT");
                this.handles = this.opcClient.SubscribeMultipleItems(
                    this.monitoredItems.ItemGroupArguments, this.OnMonitoredItemChanged);
        public void Stop()
            if (this.opcClient != null)
                this.opcClient = null;
                this.monitoredItems = null;
        private void OnMonitoredItemChanged(object sender, EasyDAItemChangedEventArgs e)
            if (this.monitoredItems != null)
                // Do something...

This class is instantiated and the method Start() is called.
Later the method Stop() is called and the class instance set to null.

What I observed with RED GATE ANTS Memory Profiler:
When I Dispose() and then set to null the EasyDAClient instance, this instance is NOT garbage because another class, RawEasyDAClient, keeps it alive.
The event handler for the subscription callbacks is also alive, and it keeps alive the TestOpcContainer instance, which is small in this sample code but relative big in the real application.
I have no way to unsubscribe the event handler itself, I can only unsubscribe the OPC ietms and dispose the EasyDAClient.

After switching the pages in the GUI N times we have N live instances of RawEasyDAClient, EasyDAClient, TestOpcContainer and possibly something else.

Would it be possible to analyse / confirm this behaviour please?

Changing the code at my side is also possible, but not right now, we are under a release.
I should also change the design.

Thanks for support,
Best regards,
Last edit: 22 Feb 2015 19:56 by support. Reason: code formatting

Please Log in or Create an account to join the conversation.

Moderators: support
Time to create page: 0.168 seconds


 Recommend this on Google