WinForms TreeView AccessViolationException
In a Windows Forms application multi-targeted to Core 6 and Framework 4.8, I'm using a TreeView control to select a subform to host in a neighboring panel control. At various points, I call the following function on the main form to build and rebuild the tree nodes:
Public Sub UpdateFormSelection(procEnt As ProcessEntity)
treeFormSelection.BeginUpdate() 'control won't repaint until EndUpdate is called
treeFormSelection.Nodes.Clear()
If Not IsListNothingOrEmpty(procEnt.Forms) Then
FormListToNodes(treeFormSelection.Nodes, procEnt.Forms) 'recurse!
End If
If (procEnt.CurrentForm IsNot Nothing) Then
treeFormSelection.SelectedNode = treeFormSelection.Nodes(procEnt.CurrentForm)
End If
treeFormSelection.EndUpdate()
End Sub
Private Sub FormListToNodes(currentNodeList As TreeNodeCollection, formList As List(Of WizardForm))
For Each form As WizardForm In formList
If form.IsSelectable Then
Dim currentNode As TreeNode = currentNodeList.Add(form.TabName)
If Not IsListNothingOrEmpty(form.Children) Then
FormListToNodes(currentNode.Nodes, form.Children) 'recurse!
currentNode.Expand()
End If
End If
Next
End Sub
At one point, I have one of the subforms change the IsSelectable property for another, then call UpdateFormSelection. The second subform appears or disappears as directed. I can change my displayed form as normal, by selecting a new node from the treeview, but within a few selections, an AccessViolationException is called.
I can't be trying to access the missing form, as I'm getting the form name to access from the treeview itself. The stack trace is as follows:
[Managed to Native Transition]
> System.Windows.Forms.Primitives.dll!Interop.User32.CallWindowProcW(System.IntPtr wndProc, System.IntPtr hWnd, Interop.User32.WM msg, System.IntPtr wParam, System.IntPtr lParam) Unknown
System.Windows.Forms.dll!System.Windows.Forms.NativeWindow.DefWndProc(ref System.Windows.Forms.Message m) Unknown
System.Windows.Forms.dll!System.Windows.Forms.Control.DefWndProc(ref System.Windows.Forms.Message m) Unknown
System.Windows.Forms.dll!System.Windows.Forms.TreeView.WmMouseDown(ref System.Windows.Forms.Message m, System.Windows.Forms.MouseButtons button, int clicks) Unknown
System.Windows.Forms.dll!System.Windows.Forms.TreeView.WndProc(ref System.Windows.Forms.Message m) Unknown
System.Windows.Forms.dll!System.Windows.Forms.Control.ControlNativeWindow.OnMessage(ref System.Windows.Forms.Message m) Unknown
System.Windows.Forms.dll!System.Windows.Forms.Control.ControlNativeWindow.WndProc(ref System.Windows.Forms.Message m) Unknown
System.Windows.Forms.dll!System.Windows.Forms.NativeWindow.Callback(System.IntPtr hWnd, Interop.User32.WM msg, System.IntPtr wparam, System.IntPtr lparam) Unknown
[Native to Managed Transition]
[Managed to Native Transition]
System.Windows.Forms.dll!System.Windows.Forms.Application.ComponentManager.Interop.Mso.IMsoComponentManager.FPushMessageLoop(System.UIntPtr dwComponentID, Interop.Mso.msoloop uReason, void* pvLoopData) Unknown
System.Windows.Forms.dll!System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Interop.Mso.msoloop reason, System.Windows.Forms.ApplicationContext context) Unknown
System.Windows.Forms.dll!System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Interop.Mso.msoloop reason, System.Windows.Forms.ApplicationContext context) Unknown
System.Windows.Forms.dll!System.Windows.Forms.Application.Run(System.Windows.Forms.ApplicationContext context) Unknown
Microsoft.VisualBasic.Forms.dll!Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun() Unknown
Microsoft.VisualBasic.Forms.dll!Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel() Unknown
Microsoft.VisualBasic.Forms.dll!Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(string[] commandLine) Unknown
I tried writing a simplified demo of the code, but it didn't cause the same error.
It should be noted that node that was selected prior to updating the treeview is deselected, and attempting to reselect it immediately after the update causes the treeview to select other nodes, apparently randomly. Selected a different node causes the form to update properly until the AccessViolationException is throw.
Additionally, I am handling the treeview DrawNode event. I set the draw mode back to normal to see if that caused the problem, but it didn't seem to have an affect.
Any thoughts on how to hunt this down?