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 ?
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.