Error: An unhandled exception of type 'System.NullReferenceException' occurred in system.windows.forms.dll
When debugging in Visual Studio .NET, the following error message is displayed when the application exits:
An unhandled exception of type 'System.NullReferenceException' occurred in system.windows.forms.dll
ActiveX control hosted in a .Net form is not disposed properly before application shut down.
Note: This exception is only thrown when debugging.
Solution or Workaround
Any .Net form hosting ActiveX control, for example, Map Control, Page Layout Control, Microsoft Web Browser, needs to be disposed properly before application shut down.
Create the form only when needed and call Form.Dispose() and set it to null immediately after the form is closed. For example, if the form needs to be shown when user clicks on a command, create the form as a local variable in ICommand::OnClick() instead of creating it as modular variable in ICommand::OnCreate().
To implement a modeless .Net form with ActiveX controls, dispose and release the form at CloseDocument and re-create the form at NewDocument or OpenDocument events.
This exception will always be displayed when implementing dockable windows in .Net using any ActiveX control during debugging even when Form.Dispose() in IDockableWindowDef::OnDestroy() is called . This is a known issue.