0

I have a class that needs to perform specific actions. Based on the input data, some dialogs might be shown. These dialogs need to be invoked with the parent window's handle so that they're centered correctly etc.

private IntPtr _parentWindow;
...
System.Windows.Forms.MessageBox.Show(System.Windows.Forms.Control.FromHandle(_parentWindow), "message");

After the parent form is ultimately closed, my class's instance still exists, with _parentWindow still assigned with a value.

Is this safe to do? Or would the GC not fully dispose the form on account of _parentWindow being populated with the handle?

MoSlo
  • 2,780
  • 4
  • 33
  • 37
  • GC never disposes any object directly. The Handle isn't reference, thus Form will be disposed when you call its Dispose method explicitly or implicitly. – Hamlet Hakobyan Nov 20 '12 at 11:21

1 Answers1

0

No, keeping the value of the window handle around won't keep the window from being destroyed or its resources from being fully released.

The GC is not responsible for releasing the unmanaged resources of the form (said resources being the window handle; this is ultimately released through DestroyWindow), and neither does it treat IntPtr members in a special manner.

Jon
  • 428,835
  • 81
  • 738
  • 806
  • Thanks, the parent window in question is pretty sizable and might be created again during user interaction. – MoSlo Nov 20 '12 at 12:32