1

So, I've never done a sizable WPF project before now, but this one has me wondering; What's up with WPF classes not implementing IDisposable? By comparison, all of the UI elements in Windows Forms implement IDisposable, to assure they get rid of the underlying handles and such.

I think the same Windows objects are under the covers there, and those resources have to be released; so, how is WPF doing that?

Is there anything I need to be doing with my WPF Window objects beyond Close()ing them?

Andrew Barber
  • 39,603
  • 20
  • 94
  • 123

1 Answers1

18

WinForms controls have handles because they are wrappers around Win32 controls. WPF controls are not (well, windows are, but the controls they host are not). The reason is that, after all, a WPF window is just a Direct3D rendering context, and all controls are just a bunch of triangles. So, they don't need to be actually registered to the OS, and therefore, they don't have handles (except windows and anything that inherits from HwndHost, of course, which are Win32 objects).

That's why there's such a sizeable interop layer between WPF and WinForms: WPF controls simply aren't Windows objects.

Etienne de Martel
  • 34,692
  • 8
  • 91
  • 111
  • 2
    I'm not familiar with WPF, but I would expect that an another important aspect is that WPF uses a weak-event handler. In some ways, I would consider that more important, since abandoned objects which hold references to Win32 controls can use finalizers to notify Windows that those controls are no longer needed, but abandoned subscribers to "strong" events from long-lived publishers will not be collected during the lifetime of those event publishers. – supercat May 14 '12 at 15:06
  • All WPF elements on the screen are ultimately backed by a HWND. When you create a WPF Window, WPF creates a top-level HWND, and uses an HwndSource to put the Window and its WPF content inside the HWND. The rest of your WPF content in the application shares that singular HWND. – JWP Oct 22 '14 at 22:33