How to debug this error when none of my code shows up in the stack?
Asked Answered
A

3

5

I'm sometimes getting the following error in my application:

Cannot use a DependencyObject that belongs to a different thread than its parent Freezable

I know how to solve this kind of error, but in that case I have no idea where it is happening, so I don't know what to fix... The exception's stack trace only contains .NET framework code, not my code (except the Main method):

   at System.Windows.Freezable.EnsureConsistentDispatchers(DependencyObject owner, DependencyObject child)
   at System.Windows.Freezable.OnFreezablePropertyChanged(DependencyObject oldValue, DependencyObject newValue, DependencyProperty property)
   at System.Windows.Freezable.OnFreezablePropertyChanged(DependencyObject oldValue, DependencyObject newValue)
   at System.Windows.Media.RenderData.PropagateChangedHandler(EventHandler handler, Boolean adding)
   at System.Windows.UIElement.RenderClose(IDrawingContent newContent)
   at System.Windows.Media.VisualDrawingContext.CloseCore(RenderData renderData)
   at System.Windows.Media.RenderDataDrawingContext.DisposeCore()
   at System.Windows.Media.DrawingContext.System.IDisposable.Dispose()
   at System.Windows.Media.RenderDataDrawingContext.Close()
   at System.Windows.UIElement.Arrange(Rect finalRect)
   at MS.Internal.Helper.ArrangeElementWithSingleChild(UIElement element, Size arrangeSize)
   at System.Windows.Controls.ContentPresenter.ArrangeOverride(Size arrangeSize)
   at System.Windows.FrameworkElement.ArrangeCore(Rect finalRect)
   at System.Windows.UIElement.Arrange(Rect finalRect)
   at System.Windows.Controls.WrapPanel.arrangeLine(Double v, Double lineV, Int32 start, Int32 end, Boolean useItemU, Double itemU)
   at System.Windows.Controls.WrapPanel.ArrangeOverride(Size finalSize)
   at System.Windows.FrameworkElement.ArrangeCore(Rect finalRect)
   at System.Windows.UIElement.Arrange(Rect finalRect)
   at MS.Internal.Helper.ArrangeElementWithSingleChild(UIElement element, Size arrangeSize)
   at System.Windows.Controls.ItemsPresenter.ArrangeOverride(Size arrangeSize)
   at System.Windows.FrameworkElement.ArrangeCore(Rect finalRect)
   at System.Windows.UIElement.Arrange(Rect finalRect)
   at System.Windows.Controls.Border.ArrangeOverride(Size finalSize)
   at System.Windows.FrameworkElement.ArrangeCore(Rect finalRect)
   at System.Windows.UIElement.Arrange(Rect finalRect)
   at System.Windows.Controls.Control.ArrangeOverride(Size arrangeBounds)
   at System.Windows.FrameworkElement.ArrangeCore(Rect finalRect)
   at System.Windows.UIElement.Arrange(Rect finalRect)
   at System.Windows.Controls.Border.ArrangeOverride(Size finalSize)
   at System.Windows.FrameworkElement.ArrangeCore(Rect finalRect)
   at System.Windows.UIElement.Arrange(Rect finalRect)
   at System.Windows.Controls.Grid.ArrangeOverride(Size arrangeSize)
   at System.Windows.FrameworkElement.ArrangeCore(Rect finalRect)
   at System.Windows.UIElement.Arrange(Rect finalRect)
   at System.Windows.ContextLayoutManager.UpdateLayout()
   at System.Windows.ContextLayoutManager.UpdateLayoutCallback(Object arg)
   at System.Windows.Media.MediaContext.InvokeOnRenderCallback.DoWork()
   at System.Windows.Media.MediaContext.FireInvokeOnRenderCallbacks()
   at System.Windows.Media.MediaContext.RenderMessageHandlerCore(Object resizedCompositionTarget)
   at System.Windows.Media.MediaContext.RenderMessageHandler(Object resizedCompositionTarget)
   at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Boolean isSingleParameter)
   at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler)
   at System.Windows.Threading.Dispatcher.WrappedInvoke(Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler)
   at System.Windows.Threading.DispatcherOperation.InvokeImpl()
   at System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(Object state)
   at System.Threading.ExecutionContext.runTryCode(Object userData)
   at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Windows.Threading.DispatcherOperation.Invoke()
   at System.Windows.Threading.Dispatcher.ProcessQueue()
   at System.Windows.Threading.Dispatcher.WndProcHook(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, Boolean isSingleParameter)
   at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler)
   at System.Windows.Threading.Dispatcher.WrappedInvoke(Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler)
   at System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Boolean isSingleParameter)
   at System.Windows.Threading.Dispatcher.Invoke(DispatcherPriority priority, Delegate method, Object arg)
   at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
   at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
   at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
   at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
   at System.Windows.Threading.Dispatcher.Run()
   at System.Windows.Application.RunDispatcher(Object ignore)
   at System.Windows.Application.RunInternal(Window window)
   at System.Windows.Application.Run(Window window)
   at System.Windows.Application.Run()
   at MyApplication.App.Main() in E:\MyApplication\MyApplication\obj\Debug\App.g.cs:line 0
   at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
   at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()

How can I track down the cause of this error ? Since the application is quite complex, manually checking all code that could be executed in another thread is not really an option...

Any advice would be appreciated !

Amalburga answered 3/9, 2010 at 14:32 Comment(1)
That is the most impressive stack trace I have ever seen.Admeasure
A
2

OK, I finally found the solution...

In one of my ViewModels, I was loading a list of brushes from the theme resources. In some situations, this ViewModel was used from a worker thread. I assumed the Freezables defined in XAML resource dictionaries were implicitly frozen, but apparently it's not the case... So I just needed to explicitly freeze them, and that fixed the problem.

Amalburga answered 3/9, 2010 at 16:7 Comment(0)
C
1

Are you adding some UI controls dynamically on a background worker's DOwork handler?

Cadence answered 3/9, 2010 at 14:37 Comment(5)
No, I'm not. The app follows the MVVM pattern, so I'm not manipulating the UI directlyAmalburga
I don't know, that's the point of my question... I'm looking for a way to get more information on that error, since the stack trace doesn't help in that caseAmalburga
see if this below post can help you as little info is on the questions. social.msdn.microsoft.com/forums/en-US/wpf/thread/…Cadence
Actually I already found the solution, but it is indeed closely related to the post you mention. Thanks ;)Amalburga
It is said that if ENDS is well than all is well too.. :) , Happy codingCadence
N
0

Have you tried Reflector Pro (there's a 14-day trial) ? You could ask it to generate debug info for WindowsBase.dll (where Freezable lives). Then you can put a breakpoint in the then part of the if statement in EnsureConsistentDispatchers. This should tell you which DependencyObjects are involved.

Are you creating DependencyObjects in another thread than the GUI thread ? Maybe in a callback method ?

Nanoid answered 3/9, 2010 at 15:4 Comment(2)
I already tried it some time ago, so my trial period is over. Anyway, I should be able to use .NET Framework source stepping, but I've been unable to make it work so far...Amalburga
I don't think I'm creating DOs in worker threads, but the code base is too large to check everything, so I'm not sure...Amalburga

© 2022 - 2024 — McMap. All rights reserved.