天天看點

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

繼續閱讀