NullReferenceException from System.Windows.Forms.ChildWindow.Callback

  • Thread starter Thread starter Daniel Carlsson
  • Start date Start date
D

Daniel Carlsson

Hello

I recently started having problems with exceptions being thrown (pretty much
at random) that look like this:

System.NullReferenceException: Object reference not set to an instance of an
object.
at System.Windows.Forms.ChildWindow.Callback(IntPtr hWnd, Int32 msg,
IntPtr wparam, IntPtr lparam)

Which is extremly hard to locate...

I found an old thread on Google which stated that the error was thrown from
a ComboBox's ChildWindow which sometimes tries to do a callback on the
ComboBox after it has been garbage collected (through a weak reference).
(Old post below)

Are there any work arounds on this bug?

Atm Im considering using an Application.ThreadException event handler and
try to drop these exceptions when they accur, but Im not sure if that would
cause other problems.

Im also wondering if anyone know if you can examine the source code for the
System.Windows.Forms package? The SSCLI doesnt cover that package from what
Ive seen. Would help if I can look through that code as well..

Thanks in advance for any help or suggestions
/Dan
 
I decided to take a look at the class in ildasm and noticed the following
(that is, unless I misunderstood, Im not very good with IL at all).
The ChildWindow's CallBack method does use a Try Catch around the callback.
And in that said catch block it calls the Application.OnThreadException
method and then promptly ignores the exception.

Thus this bug should only appear if your using an event handler on
Application.ThreadException to begin with. Which I am, it tracks any
exceptions that arent being caught and logs them on a machine that I cant do
direct debugging on.. In other words, it only appears when Im running it on
that machine..

Im not quite certain about this though, so I would appreciate if anyone who
knows IL better could check the following method and let me know if I
understood it correctly.

And if anyone happens to know if this is common practice in Microsofts code?
If so I wont be able to use that EventHandler as it would trigger on
exceptions that never would have caused any problems..

Thanks in advance
/Dan

..method public hidebysig instance native int
Callback(native int hWnd,
int32 msg,
native int wparam,
native int lparam) cil managed
{
// Code size 53 (0x35)
.maxstack 4
.locals (valuetype System.Windows.Forms.Message V_0,
class [mscorlib]System.Exception V_1)
IL_0000: ldarg.1
IL_0001: ldarg.2
IL_0002: ldarg.3
IL_0003: ldarg.s lparam
IL_0005: call valuetype System.Windows.Forms.Message
System.Windows.Forms.Message::Create(native int,

int32,

native int,

native int)
IL_000a: stloc.0
.try
{
IL_000b: ldarg.0
IL_000c: ldfld class [mscorlib]System.WeakReference
System.Windows.Forms.ComboBox/ChildWindow::reference
IL_0011: callvirt instance object
[mscorlib]System.WeakReference::get_Target()
IL_0016: castclass System.Windows.Forms.ComboBox
IL_001b: ldloca.s V_0
IL_001d: callvirt instance void
System.Windows.Forms.ComboBox::ChildWndProc(valuetype
System.Windows.Forms.Message&)
IL_0022: leave.s IL_002d
} // end .try
catch [mscorlib]System.Exception
{
IL_0024: stloc.1
IL_0025: ldloc.1
IL_0026: call void
System.Windows.Forms.Application::OnThreadException(class
[mscorlib]System.Exception)
IL_002b: leave.s IL_002d
} // end handler
IL_002d: ldloca.s V_0
IL_002f: call instance native int
System.Windows.Forms.Message::get_Result()
IL_0034: ret
} // end of method ChildWindow::Callback
 
Hi,

Windows forms is not a part of CLI, so it is not in the SSCLI
implementation.
I think this code had handled this exception by eaten it, the
ThreadException handler is just to inform there is an exception thrown on
this thread, base on my understanding, you may safely ignore this
NullReferenceException.
If you met some problem caused by this exception, please feel free to reply
this thread with more detail description.

Thanks!

Best regards,

Ying-Shen Yu [MSFT]
Microsoft Community Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
This mail should not be replied directly, please remove the word "online"
before sending mail.
 
Hi Daniel,

Dev team confirmed this is a known issue,
Current recommendation from the dev team is to make sure all forms are
properly disposed after use. So if you are using ComboBox on some modal
forms you may need to dispose the form after closing it explicitly.

Thanks!

Best regards,

Ying-Shen Yu [MSFT]
Microsoft Community Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
This mail should not be replied directly, please remove the word "online"
before sending mail.
 
Hi again,

I checked and found a few windows that I had forgotten to use Dispose on so
fixed that.
Im using an Form.Closed eventhandler on MDI forms to dispose them when they
are closed (some are used both as modal forms and MDI forms). So now all
forms should be Disposed.

Going to have to see if that fixes the problem, Im keeping my filtering code
for now though, incase it doesnt help (and will log if it happens in that
code).

Thanks for the help
/Dan
 
Back
Top