0

Recently I working on WPF application where I was getting exception related to Out of memory error. Here is the exception :

System.OutOfMemoryException: Insufficient memory to continue the execution of the program.
   at System.Windows.Media.Composition.DUCE.Channel.SyncFlush()
   at System.Windows.Interop.HwndTarget.UpdateWindowSettings(Boolean enableRenderTarget, Nullable`1 channelSet)
   at System.Windows.Interop.HwndTarget.UpdateWindowPos(IntPtr lParam)
   at System.Windows.Interop.HwndTarget.HandleMessage(WindowMessage msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Interop.HwndSource.HwndTargetFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
   at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
   at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)

In above I can see HwndTarget exception , in my application I have implemented SourceInitialized where I used below code

        void myClass_SourceInitialized(object sender, EventArgs e)
        {
            Log.Info("myClass_SourceInitialized ");
            System.Windows.Interop.HwndSource source =                        System.Windows.Interop.HwndSource.FromHwnd(new System.Windows.Interop.WindowInteropHelper(this).Handle);
            source.AddHook(new System.Windows.Interop.HwndSourceHook(WndProc));
            var ss = source.CompositionTarget;
            ss.RenderMode = RenderMode.SoftwareOnly;
            Log.Info(ss.TransformFromDevice);
        }

        private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
        {
            if (msg == WM_NCLBUTTONDBLCLK)
            {
                handled = true;
            }

            if (msg == WM_SYSCOMMAND)
            {
                int command = wParam.ToInt32() & 0xfff0;
                if (command == SC_MOVE)
                {
                    handled = true;
                }
            }
            return IntPtr.Zero;
        }

I want to understand what is the exact use of above code and is the exception i got is related to code ?

1
  • The exception is related to your machine running out of memory. It could be the application or it could be another application that is using a lot of memory, or you just didn't purchase a lot of memory for your machine. You could increase the Virtual memory which uses disk for memory when you run out of memory. See : windowscentral.com/… Commented Apr 11, 2023 at 10:16

1 Answer 1

0

In fact, this exception is very misleading and most probably NOT related to your machine running out of memory or your code!

Here are the various root causes that have triggered this exception in our code base over the decades:

Hope this helps.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.