天天看点

Silverlight实用窍门系列:45.Silverlight下使用WinDbg调试应用程序和查看异常情况

    在本节中我们将讲述如何通过WinDbg工具对Silverlight应用程序进行调试,我们可以判断其无效过期的引用或者事件,将其释放掉,以达到及时释放内存的作用。

        然后我们准备一个最简单的Silverlight应用程序,其Xaml代码如下:

<UserControl x:Class="SLDbg.MainPage" 

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 

xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 

xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 

mc:Ignorable="d" 

d:DesignHeight="300" d:DesignWidth="400"> 

<Grid x:Name="LayoutRoot" Background="White"> 

<Button x:Name="btnClick" Width="70" Height="40" Content="点击我"></Button> 

</Grid> 

</UserControl> 

        其Xaml.cs文件如下,绑定了一个事件:

using System; 

using System.Collections.Generic; 

using System.Linq; 

using System.Net; 

using System.Windows; 

using System.Windows.Controls; 

using System.Windows.Documents; 

using System.Windows.Input; 

using System.Windows.Media; 

using System.Windows.Media.Animation; 

using System.Windows.Shapes; 

namespace SLDbg 

public partial class MainPage : UserControl 

public MainPage() 

InitializeComponent(); 

this.btnClick.Click += new RoutedEventHandler(btnClick_Click); 

void btnClick_Click(object sender, RoutedEventArgs e) 

MessageBox.Show("1111"); 

        第一、对上面名为SLDbg,然后在web端中右键点击浏览,然后打开新安装好的WinDbg工具。

        第二、在WinDbg工具中,“File”->"Attach to  Process",在打开的页面中选中需要调试的当前运行iexplorer程序

(可以根据这个IE的进程ID判断,任务管理器的进程可以选中需要查看的进程ID)。

<a target="_blank" href="http://blog.51cto.com/attachment/201204/231527976.jpg"></a>

        第三、将需要跟踪的进程附加到WinDbg中之后,我们首先要引入Sos.dll进行调试,所以在WinDbg的命令输入: .load C:\Program Files\Microsoft Silverlight\4.0.50826.0\sos.dll

<a target="_blank" href="http://blog.51cto.com/attachment/201204/231527974.jpg"></a>

        第四、使用命令:!dumpheap -stat -type SLDbg,开始调试名为SLDbg的Silverlight应用程序,其界面如下:

<a target="_blank" href="http://blog.51cto.com/attachment/201204/231527678.jpg"></a>

        第五、在上图中我们可以看到XX对象的内存地址是BBBBBB,所以我们输入以下命令:!dumpheap -MT BBBBBB去查看该对象的使用情况如下图所示:

<a target="_blank" href="http://blog.51cto.com/attachment/201204/231756708.jpg"></a>

        第六、在使用命令:!dumpheap -stat -type SLDbg的时候,如果查看到有异常的话,查看其内存地址为MMMMMM,则通过命令 !pe MMMMMM,可以查看其具体出错原因。

        本实例采用VS2010+Silverlight 4.0编写。

本文转自程兴亮 51CTO博客,原文链接:http://blog.51cto.com/chengxingliang/826477

继续阅读