Exception in ListView.OnHandleDestroyed

C

Christian Wilhelm

Hi!

Sometimes (not reproduceable) I'm getting an NullRefException from
ListView.OnHandleDestroyed()-Method.
Can somebody tell me, why ListViewItemCollection.get_Item() is being
accessed, when the Handle of the ListView ist already destroyed?
....
3) System.NullReferenceException
2) ListViewItemCollection.get_Item(Int32 displayIndex)
1) ListView.OnHandleDestroyed(EventArgs e)
....

Thank you very much,
Christian

System.NullReferenceException: Der Objektverweis wurde nicht auf eine
Objektinstanz festgelegt.
at System.Windows.Forms.ListViewItemCollection.get_Item(Int32 displayIndex)
at System.Windows.Forms.ListView.OnHandleDestroyed(EventArgs e)
at System.Windows.Forms.Control.WmDestroy(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ListView.WndProc(Message& m)
at System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32
msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.CallWindowProc(IntPtr wndProc,
IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at System.Windows.Forms.NativeWindow.DefWndProc(Message& m)
at System.Windows.Forms.Control.DefWndProc(Message& m)
at System.Windows.Forms.Control.WmClose(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ListView.WndProc(Message& m)
at System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32
msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at
System.Windows.Forms.ComponentManager.System.Windows.Forms.UnsafeNativeMethods+IMsoComponentManager.FPushMessageLoop(Int32
dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.ThreadContext.RunMessageLoopInner(Int32 reason,
ApplicationContext context)
at System.Windows.Forms.ThreadContext.RunMessageLoop(Int32 reason,
ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at client.clientMDI.Main(String[] args) in D:\blablabla.cs:line 111
 
G

Guest

Hi Christian,

ListViewItemCollection.get_Item() is being accessed during OnHandleDestroyed
because the ListView class copies your items out of a private hashtable into
an arraylist. The hashtable is used as part of a windows interop mechanism,
so your items would be unavailable to you after the Handle was destroyed
unless they were copied into the arraylist.

As for why the NullReferenceException is occurring, I don't know - I'd need
know more about what you're doing and how you're using the ListView.

If you wanted a quick fix, you could override OnHandleDestroyed and clear
your items before calling base - that may do the trick, although I think it
would be nicer to find out what's actually going on :)

Hope this helps.

Regards,
Matt Garven

Christian Wilhelm said:
Hi!

Sometimes (not reproduceable) I'm getting an NullRefException from
ListView.OnHandleDestroyed()-Method.
Can somebody tell me, why ListViewItemCollection.get_Item() is being
accessed, when the Handle of the ListView ist already destroyed?
....
3) System.NullReferenceException
2) ListViewItemCollection.get_Item(Int32 displayIndex)
1) ListView.OnHandleDestroyed(EventArgs e)
....

Thank you very much,
Christian

System.NullReferenceException: Der Objektverweis wurde nicht auf eine
Objektinstanz festgelegt.
at System.Windows.Forms.ListViewItemCollection.get_Item(Int32 displayIndex)
at System.Windows.Forms.ListView.OnHandleDestroyed(EventArgs e)
at System.Windows.Forms.Control.WmDestroy(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ListView.WndProc(Message& m)
at System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32
msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.CallWindowProc(IntPtr wndProc,
IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at System.Windows.Forms.NativeWindow.DefWndProc(Message& m)
at System.Windows.Forms.Control.DefWndProc(Message& m)
at System.Windows.Forms.Control.WmClose(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ListView.WndProc(Message& m)
at System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32
msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at
System.Windows.Forms.ComponentManager.System.Windows.Forms.UnsafeNativeMethods+IMsoComponentManager.FPushMessageLoop(Int32
dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.ThreadContext.RunMessageLoopInner(Int32 reason,
ApplicationContext context)
at System.Windows.Forms.ThreadContext.RunMessageLoop(Int32 reason,
ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at client.clientMDI.Main(String[] args) in D:\blablabla.cs:line 111
 
C

Christian Wilhelm

Hi Matt!

Thank you very much for your Reply!
I will try this approach and when I found out what's happening, I will post
in this thread again.

Thank you,
Christian

Matt Garven said:
Hi Christian,

ListViewItemCollection.get_Item() is being accessed during
OnHandleDestroyed
because the ListView class copies your items out of a private hashtable
into
an arraylist. The hashtable is used as part of a windows interop
mechanism,
so your items would be unavailable to you after the Handle was destroyed
unless they were copied into the arraylist.

As for why the NullReferenceException is occurring, I don't know - I'd
need
know more about what you're doing and how you're using the ListView.

If you wanted a quick fix, you could override OnHandleDestroyed and clear
your items before calling base - that may do the trick, although I think
it
would be nicer to find out what's actually going on :)

Hope this helps.

Regards,
Matt Garven

Christian Wilhelm said:
Hi!

Sometimes (not reproduceable) I'm getting an NullRefException from
ListView.OnHandleDestroyed()-Method.
Can somebody tell me, why ListViewItemCollection.get_Item() is being
accessed, when the Handle of the ListView ist already destroyed?
....
3) System.NullReferenceException
2) ListViewItemCollection.get_Item(Int32 displayIndex)
1) ListView.OnHandleDestroyed(EventArgs e)
....

Thank you very much,
Christian

System.NullReferenceException: Der Objektverweis wurde nicht auf eine
Objektinstanz festgelegt.
at System.Windows.Forms.ListViewItemCollection.get_Item(Int32
displayIndex)
at System.Windows.Forms.ListView.OnHandleDestroyed(EventArgs e)
at System.Windows.Forms.Control.WmDestroy(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ListView.WndProc(Message& m)
at System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,
Int32
msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.CallWindowProc(IntPtr
wndProc,
IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at System.Windows.Forms.NativeWindow.DefWndProc(Message& m)
at System.Windows.Forms.Control.DefWndProc(Message& m)
at System.Windows.Forms.Control.WmClose(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ListView.WndProc(Message& m)
at System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,
Int32
msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at
System.Windows.Forms.ComponentManager.System.Windows.Forms.UnsafeNativeMethods+IMsoComponentManager.FPushMessageLoop(Int32
dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.ThreadContext.RunMessageLoopInner(Int32 reason,
ApplicationContext context)
at System.Windows.Forms.ThreadContext.RunMessageLoop(Int32 reason,
ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at client.clientMDI.Main(String[] args) in D:\blablabla.cs:line 111
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top