WPF的TreeView执行ExpandSubtree时抛出异常System.NullReferenceException

??

最近拿到一个dump,有应用崩溃,通过查看dump,异常信息如下:

0:012> !pe

Exception object: 0000000005187278

Exception type:   System.NullReferenceException

Message:          Object reference not set to an instance of an object.

InnerException:   <none>

StackTrace (generated):

SP               IP               Function

000000002144DB50 000007FEEDF84989 PresentationFramework_ni!System.Windows.Controls.TreeViewItem.ExpandRecursive(System.Windows.Controls.TreeViewItem)+0x119

000000002144DC10 000007FEEDF84B46 PresentationFramework_ni!System.Windows.Controls.TreeViewItem.ExpandRecursive(System.Windows.Controls.TreeViewItem)+0x2d6

000000002144DCD0 000007FEEDE574B8 PresentationFramework_ni!System.Windows.Controls.TreeView.ExpandSubtree(System.Windows.Controls.TreeViewItem)+0x18

000000002144DD00 000007FEEDE572F5 PresentationFramework_ni!System.Windows.Controls.TreeView.OnKeyDown(System.Windows.Input.KeyEventArgs)+0x135

000000002144DD40 000007FEF116B1E3 PresentationCore_ni!System.Windows.RoutedEventArgs.InvokeHandler(System.Delegate, System.Object)+0x53

000000002144DDA0 000007FEF116A991 PresentationCore_ni!System.Windows.EventRoute.InvokeHandlersImpl(System.Object, System.Windows.RoutedEventArgs, Boolean)+0x271

000000002144DFE0 000007FEF115408E PresentationCore_ni!System.Windows.UIElement.RaiseEventImpl(System.Windows.DependencyObject, System.Windows.RoutedEventArgs)+0x14e

000000002144E070 000007FEF114FF66 PresentationCore_ni!System.Windows.UIElement.RaiseTrustedEvent(System.Windows.RoutedEventArgs)+0x96

000000002144E0D0 000007FEF117C921 PresentationCore_ni!System.Windows.Input.InputManager.ProcessStagingArea()+0x471

000000002144E160 000007FEF117C42B PresentationCore_ni!System.Windows.Input.InputManager.ProcessInput(System.Windows.Input.InputEventArgs)+0xab

000000002144E1B0 000007FEF117C1BC PresentationCore_ni!System.Windows.Interop.HwndKeyboardInputProvider.ReportInput(IntPtr, System.Windows.Input.InputMode, Int32, System.Windows.Input.RawKeyboardActions, Int32, Boolean, Boolean, Int32)+0x1bc

!pe即PrintException,从异常堆栈信息上可以看到当时在TreeView上按了某个键导致TreeView要展开TreeViewItem,然后循环时抛出空指针异常。

接下来就是查看当时传递的KeyDown事件是哪个,查看google后有的人说通过!clrstack -p可以看到函数参数的地址, 但试过后发现由于是Release版本,这个windbg命令并不起效。

只能用笨办法,通过!dso来查看当时整个线程所有堆栈变量, 如下:

0:012> ~12e !dso
OS Thread Id: 0xba4 (12)
RSP/REG          Object           Name
00000000214479B0 0000000005187278 Syste.NullReferenceException

000000002144DCB0 00000000051840c8 System.Windows.Input.KeyEventArgs
000000002144DCC0 0000000002d728f8 System.Windows.Controls.TreeView
000000002144DCD0 00000000041c1188 System.Windows.Controls.TreeViewItem

上面12是抛异常的clr线程号,可以通过~*kb查看所有现场的堆栈,就可以知道具体线程号

从上面命令的部分信息可以看到当时KeyEventArgs的地址。

接下来通过!do命令查看具体信息

012> !do 00000000051840c8

Name:        System.Windows.Input.KeyEventArgs

MethodTable: 000007fef1283900

EEClass:     000007fef0ee4de0

Size:        80(0x50) bytes

File:        C:\Windows\Microsoft.Net\assembly\GAC_64\PresentationCore\v4.0_4.0.0.0__31bf3856ad364e35\PresentationCore.dll

Fields:

MT    Field   Offset                 Type VT     Attr            Value Name

000007fef5070c78  40001c7       80     System.EventArgs  0   shared           static Empty

>> Domain:Value  00000000008083f0:0000000002bd96d0 <<

000007fef1279088  40012ff        8 ...ndows.RoutedEvent  0 instance 0000000002ba43d0 _routedEvent

000007fef5055ac8  4001300       10        System.Object  0 instance 0000000002d728f8 _source

000007fef5055ac8  4001301       18        System.Object  0 instance 00000000041c1188 _originalSource

000007fef43990a8  4001302       20 ...lized.BitVector32  1 instance 00000000051840e8 _flags

000007fef128b320  40016ce       28 ...Input.InputDevice  0 instance 0000000002bc2f00 _inputDevice

000007fef505c858  40016cf     1460         System.Int32  1   static        396682259 _timestamp

000007fef3161bf0  40018ba       38         System.Int32  1 instance               84 _realKey

000007fef3161bf0  40018bb       3c         System.Int32  1 instance               84 _key

000007fef1283da8  40018bc       30 ...resentationSource  0 instance 0000000003f8d7a0 _inputSource

000007fef505d688  40018bd       44       System.Boolean  1 instance                0 _isRepeat

000007fef505c858  40018be       40         System.Int32  1 instance               55 _scanCode

000007fef505d688  40018bf       45       System.Boolean  1 instance                0 _isExtendedKey

从上面可以看到当时按键Key的值是84,

通过查看enum类型的Key源码,发现84是*

namespace System.Windows.Input

{

[ValueSerializer(typeof (KeyValueSerializer))]

[TypeConverter(typeof (KeyConverter))]

public enum Key

{

NumPad9 = 83,

    Multiply = 84,

Add = 85,

Separator = 86,

Subtract = 87,

Decimal = 88,

Divide = 89,

通过查看TreeView的代码也可以看出Multiply的快捷键作用是ExpandSubTree

case Key.Multiply:

if (ExpandSubtree(_selectedContainer))

{

e.Handled = true;

}

break;

结论:

*是TreeView控件的默认系统快捷键,用来展开TreeViewItem,当TreeView的Style有问题时,导致递归展开子项时抛了异常。

解决办法:

修改Style或者屏蔽快捷键。

时间: 2024-08-04 23:50:12

WPF的TreeView执行ExpandSubtree时抛出异常System.NullReferenceException的相关文章

WebApi 数据保护操作未成功。这可能是由于未为当前线程的用户上下文加载用户配置文件导致的。当线程执行模拟时,可能会出现此情况。&quot;,&quot;ExceptionType&quot;:&quot;System.Security.Cryptography.CryptographicException&quot;,&quot;StackTrace

在调用System.Security.Cryptography.ProtectedData.Protect方法来保护私密信息时,IIS可能会报以下错误:CryptographicException: 数据保护操作未成功.这可能是由于未为当前线程的用户上下文加载用户配置文件导致的.当线程执行模拟时,可能会出现此情况. 解决方法:1.打开应用程序池2.在应用程序上右键选择高级设置3.进程模型下设置“加载用户配置文件”为True

[WPF实用技巧]如何使WPF的TreeView节点之间有连线

示例代码:TreeViewEx.zip 原文地址:http://www.codeproject.com/Tips/673071/WPF-TreeView-with-WinForms-Style-Fomat   Introduction WPF default TreeView is very good, but many people still want it to have lines join each of its child elements, like Windows Forms T

WPF加载Winform窗体时 报错:子控件不能为顶级窗体

一.wpf项目中引用WindowsFormsIntegration和System.Windows.Forms 二.Form1.Designer.cs 的 partial class Form1 设置为:public partial class Form1 三.代码如下: XXXX.Form1 Zhuwindow = new XXXX.Form1(); Zhuwindow.TopLevel = false; Zhuwindow.FormBorderStyle = System.Windows.Fo

RobotFramework-----第二次执行case时,log窗口未显示数据

第二次执行case时,log窗口未显示数据,如下图 原因:case运行时,是使用IE或是chrome浏览器,运行一次后IEDriverServer.exe或chromedriver.exe进程仍在运行中. 解决方案: 方法一:在任务管理器中,将后IEDriverServer.exe或chromedriver.exe进程结束 方法二: 将下面代码保存为批处理,每次运行后手动运行一下: taskkill /f /im  chromedriver.exe taskkill /f /im  IEDriv

InteropBitmap指定内存,绑定WPF的Imag控件时刷新问题。

1.InteropBitmap指定内存,绑定WPF的Imag控件的Source属性 创建InteropBitmap的时候,像素的格式必须为PixelFormats.Bgr32, 如果不是的话在绑定到Image控件的Source属性,刷新新界面(BitmapSource.Invalidate())的时候会造成内存泄露. 2. 内存映射: //内存共享类 internal class Win32Mess { [DllImport("VCamBridge.dll", EntryPoint =

android 修改listview中adapter数据时抛出异常java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive a notification问题

近日在做项目时遇到非必现crush,具体异常信息为: // Short Msg: java.lang.IllegalStateException // Long Msg: java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive a notification. Make sure the content of your adapter is not mo

WPF MVVM TreeView 实现 右键选中 右键菜单

1.非MVVM模式:下载源代码WpfApplication1.zip <TreeView Height="200" PreviewMouseRightButtonDown="TreeViewItem_PreviewMouseRightButtonDown" HorizontalAlignment="Left" Margin="12,0,0,0" Name="treeView1" VerticalAli

php -- PHP在linux上执行外部命令,system(),exec(),shell_exec()

目录:一.PHP中调用外部命令介绍二.关于安全问题三.关于超时问题四.关于PHP运行linux环境中命令出现的问题 一.PHP中调用外部命令介绍 在PHP中调用外部命令,有三种方法: 1. 调用专门函数 2. 反引号 3. popen()函数打开进程 方法一:调用PHP提供的专门函数(四个): PHP提供4个专门的执行外部命令的函数:exec(), system(), passthru(), shell_exec() 1)exec() 原型: string exec ( string $comm

使用dynamic引发的异常:无法对 null 引用执行运行时绑定

今天上午运营反映有商户的账单没有生成. 查看日志,在批量生成账单服务执行过程中,因为如下异常而中断了: 跑批异常 Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: 无法对 null 引用执行运行时绑定 在 CallSite.Target(Closure , CallSite , Object ) 在 System.Dynamic.UpdateDelegates.UpdateAndExecute1[T0,TRet](CallSite si