NullReferenceException Occurs in Windows Forms Application Running on .NET Framework 2.0 Service Pack 1

You are running a Microsoft .NET 2.0 Windows Forms application that contains an ActiveX control on a computer with Microsoft .NET 2.0 Framework Service Pack 1 installed.

 

Result

You receive the following unhandled exception and call stack.

System.NullReferenceException: Object reference not set to an instance of an object.

at System.Windows.Forms.AxHost.OleInterfaces.AttemptStopEvents(Object trash)
at System.Threading._ThreadPoolWaitCallback.WaitCallback_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading._ThreadPoolWaitCallback.PerformWaitCallbackInternal(_ThreadPoolWaitCallback tpWaitCallBack)
at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback(Object state)


Cause

This is due to a bug in .NET Framework 2.0 Service Pack 1.

During a garbage collection of a Windows Form containing ActiveX controls, the Finalizer for the System.Windows.Forms.AxHost.OleInterfaces type executes. This calls the System.Windows.Forms.AxHost.OleInterfaces.AttemptStopEvents method to release COM connection points for ActiveX controls. The AttemptStopEvents method references an internal member without checking if it is null (Nothing). In some cases, this member may be null, and results in the System.NullReferenceException.


Resolution

You can work around this problem by adding code to the FormClosing event handler of the Windows Form to suppress finalization of the System.Windows.Forms.AxHost.OleInterfaces type. The following sample code demonstrates how to do this. In the following sample code, MyActiveXControl represents the name of an ActiveX control sited on the Windows Form.

C# Sample Code
=============

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (MyActiveXControl != null)
{
System.Reflection.FieldInfo fi = typeof(System.Windows.Forms.AxHost).GetField(“oleSite”,
System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
object o = fi.GetValue(MyActiveXControl);
GC.SuppressFinalize(o);
}
}

Visual Basic .NET Sample Code
========================

Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As FormClosingEventArgs) Handles Form1.FormClosing
If  Not IsNothing(MyActiveXControl) Then
Dim fi As System.Reflection.FieldInfo
Dim o As Object
fi = GetType(System.Windows.Forms.AxHost).GetField(“oleSite”, _
System.Reflection.BindingFlags.Instance Or System.Reflection.BindingFlags.NonPublic)
o = fi.GetValue(MyActiveXControl)
GC.SuppressFinalize(o)
End If
End Sub


DISCLAIMER

MICROSOFT AND/OR ITS SUPPLIERS MAKE NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY, RELIABILITY OR ACCURACY OF THE INFORMATION CONTAINED IN THE DOCUMENTS AND RELATED GRAPHICS PUBLISHED ON THIS WEBSITE (THE “MATERIALS”) FOR ANY PURPOSE. THE MATERIALS MAY INCLUDE TECHNICAL INACCURACIES OR TYPOGRAPHICAL ERRORS AND MAY BE REVISED AT ANY TIME WITHOUT NOTICE.

TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW, MICROSOFT AND/OR ITS SUPPLIERS DISCLAIM AND EXCLUDE ALL REPRESENTATIONS, WARRANTIES, AND CONDITIONS WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING BUT NOT LIMITED TO REPRESENTATIONS, WARRANTIES, OR CONDITIONS OF TITLE, NON INFRINGEMENT, SATISFACTORY CONDITION OR QUALITY, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, WITH RESPECT TO THE MATERIALS.



APPLIES TO
Microsoft .NET Framework 2.0

——————————————–

Microsoft Knowledge Base Article

This article contents is Microsoft Copyrighted material.
Microsoft Corporation. All rights reserved. Terms of Use | Trademarks


You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

AddThis Social Bookmark Button

Leave a Reply

*
To prove that you're not a bot, enter this code
Anti-Spam Image