Memory Leak in ImageViewer C#

Hello,

We are developing an application where multiple windows with camera videos can be opened. For this matter, we launch different threads with windows were the component ImageViewerWpfControl is being used. When we close these windows, we are disconnecting and closing the ImageViewers using this method, but we are still having the memory leaks:

Any idea of why is this happening?

Thank you.

Do you have an indication that it is the ImageViewerWpfControl that is leaking or something else?

If ImageViewerWpfControl is leaking we would need to reproduce and debug here at Milestone, please provide a minimal implementation application (source code project) for us to analyze. (I can provide an upload space that is not public if you do not want to share it in this public forum, let me know.)

Hello again Bo,

Could you provide a private space so we can share our demo with you?

Thank you.

I have created a support case MSC2088600 for you. Information you put or upload on the case will be visible in Milestone Technical Support, and only there.

Hello,

I’m not seeing the support case MSC2088600 anywhere, where can I found this case?

Thank you.

You should have received an email from the Milestone Support Case system, there should be a link to follow. Let me know if you have it.

Hello again! No emails have arrived from the Milestone Suport Case system.

I tried to resend from the Support Case System, and I got no response so I guess it isn’t working for you. Next step I have now sent another email from another system where you can upload.

Hello, perfect. I have uploaded the code in the folder that you have shared with us. Another hint is that when we close a window with camera video, this message appears:

Captura de pantalla 2025-06-23 145009

Hello,

We are planning to prepare a brief demonstration in which we will execute the various image viewers in the same manner as we are currently doing, but without any additional components. If we observe that there are no further memory leaks, we will update this post accordingly. Otherwise, we will share the relevant code.

We would appreciate it if you could set up a private space for sharing the code, in case the memory leaks persist.

Thank you!

Can you please tell me how I am supposed to test with the app you sent me?

What I have observed is that when twice as many ImageViewerControls are open there is a bigger memory consumption. I am guessing that was not the observation you wanted me to make.

Please elaborate on how to test and include your observations and conclusions from what you observed.

Hello, what we did with this demo are these steps:

  1. The first set of windows is opened.
  2. Close the opened windows.
  3. Press enter in the console and see the performance stats.
  4. Press enter again and the second set of windows is opened.
  5. Close these windows.
  6. Press enter and the new performance stats are shown.

We noticed that the second printed stats are higher than the first stats.

I am surprised to follow your steps and find

The numbers are not the same every time but not really indicating a leak.

On our end, we are observing a memory accumulation issue:

Furthermore, the more windows we open, the more memory continues to accumulate. We are conducting these tests within a virtual environment—could this be contributing to the issue?

Hi,

I have tried several times with your app and am unable to conclusively see any leaks. However, a few issues worth noting: The Closed event is triggered after the window is already closing, which is why there are occasional errors during shutdown of the windows (if the viewitem is trying to show an image in a window that no longer exists). Consider instead using the Closing event. Another issue worth noting is that you are creating leaks in the event handler on the Closed event. To avoid this you could use a private method, like this:

                void Disposer(object s, CancelEventArgs ea)
                {
                    foreach (var child in totalGrid.Children)
                    {
                        if (child is ViewItem viewItem)
                        {
                            viewItem.disconnectImageViewerComponent();
                        }
                        else
                        {
                            //Do nothing
                        }
                    }
                    Dispatcher.CurrentDispatcher.InvokeShutdown();
                    window.Closing -= Disposer;
 
                }
 
                window.Closing += Disposer;
                window.Show();
                Dispatcher.Run();

If you wanted to conclusively prove or disprove that there might be memory leaks in the ImageViewerWpfControl, you could try making a version of the application that continues to open and close windows, logging the memory use to somewhere more permanent than the console. Letting that run overnight should give a better indication.

Best regards,

Simon

Hi! We tested with the private method and the window.Closing event and the result are the same. Maybe we are getting memory accumulation because we are testing in a virtual environment? Could you please provide information about the hardware and system resources of the machine where you are testing this?

We will do also a test running the program all night closing a opening windows.

Thanks!