Search code examples
wpfwinapicom

WindowInteropHelper.Handle — do I need to release it?


In WPF I'm getting IntPtr handle using this code:

IntPtr mainWindowHandle = new WindowInteropHelper(Application.Current.MainWindow).Handle

When I finish using this handle, do I need to release it in anyway (e.g. using Marshal.FreeHGlobal() method) ?

EDIT: I was thinking about Marshal.FreeHGlobal(), not Marshal.Release(), sorry!


Solution

  • This is not in any way related to COM, Marshal.Release() does not apply. You simply get a copy of the native window handle, it does not have to be released. You need to stop using the handle when the window is destroyed. Signaled by the Window.Close event.