6

The following code crashes with an exception :

MyWindow wnd = new MyWindow();
wnd.Show(); //here an exception occurs

Exception is rather strange but as I understand its a bug in .net

System.ComponentModel.Win32Exception (0x80004005): The operation completed successfully
   at MS.Win32.UnsafeNativeMethods.GetDC(HandleRef hWnd)
   at System.Windows.Interop.HwndTarget..ctor(IntPtr hwnd)
   at System.Windows.Interop.HwndSource.Initialize(HwndSourceParameters parameters)
   at System.Windows.Interop.HwndSource..ctor(HwndSourceParameters parameters)
   at System.Windows.Window.CreateSourceWindow(Boolean duringShow)
   at System.Windows.Window.CreateSourceWindowDuringShow()
   at System.Windows.Window.SafeCreateWindowDuringShow()
   at System.Windows.Window.ShowHelper(Object booleanBox)
   at System.Windows.Window.Show()

MyWindow object is a window with some vector graphics inside, but not too much. Also, it happens when 10-20 MyWindow objects have been opened and closed already.

Solution: The reason was a leak of GDI objects.They were creating in my low level code containing a mistake. So, the problem had no concern to MyWindow object.

Cœur
  • 37,241
  • 25
  • 195
  • 267
HelloWorld
  • 1,061
  • 2
  • 15
  • 25
  • 1
    If you are creating windows not from the Main thread, you might have got too many `Dispatchers` alive. Try following the workaround proposed [on Connect issue](http://connect.microsoft.com/VisualStudio/feedback/details/620588/system-componentmodel-win32exception-0x80004005-not-enough-storage-is-available-to-process-this-command). Perhaps your windows have spent too much resources. – Pavel Gatilov Dec 11 '11 at 16:28
  • What do you do in the constructor of MyWindow? Do you override the show method? – rekire Dec 11 '11 at 16:33
  • @rekire No, I don't override.The only line in constructor is InitializeComponent() – HelloWorld Dec 11 '11 at 16:44
  • I have the same problem but in a different context. Noticed @rekire mentioned the Show() method. Since I am doing MVVM, I am overriding `Startup()` in my `App.xaml.cs` and using `window.Show()` in it. Is it possible that this is causing the problem when I am trying to use handler for BluetoothWin32Authentication ? – Mehrad Apr 04 '14 at 03:57
  • I actually my one liner in a new project with a single button and no MVVM no nothing. and still get the same error. – Mehrad Apr 04 '14 at 04:21

2 Answers2

11

It doesn't bomb on a winapi error code, the actual error code is E_FAIL, a COM error code. Which is very unhelpful to diagnose anything, it doesn't mean anything more than "couldn't do it, no idea why". How GetDC() can produce that error code is very hard to guess, I suspect it is environmental with something hooking the winapi function. Perhaps something similar to remote desktop or a screen recorder. Do try running this on another machine.

The "normal" reason for GetDC() failure is a handle leak. Windows stops giving a process more handles when it has consumed 10,000 of them already. Something you can diagnose with TaskMgr.exe, Processes tab. View + Select Columns and tick Handles, USER Objects and GDI Objects. First check the list of processes and verify that you don't have a process that consumes a lot of them. The total number of GDI Objects for all processes in a session is limited by the session pool size. Next run your program and keep an eye on the values for your process.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • Why GC doesn't take care of handles? Why GC is not being executed until the number of Handles reach to 10k? – Zafar Mar 03 '14 at 16:42
  • 1
    It is not the job of the GC to manage handles, it only manages memory. It is the job of a *finalizer* to take care of releasing a handle. Programs fail when they don't run soon enough and the programmer ignored the Dispose() method. – Hans Passant Mar 03 '14 at 16:52
0

We have this problem in our project too...

we were putting all view and view models to stack and then showing one by one them. Solution was to show elements one by one without making the stack.

Nazar Harasym
  • 43
  • 2
  • 8