Note that there are some explanatory texts on larger screens.

plurals
  1. PODispose of WPF Window
    text
    copied!<p>I have a WPF Window that I show with Window.Show(). When I click X the form closes. But it is still in memory and the GC never comes and cleans it up. Also I do not have any refrences/handles to it.</p> <p>does any one know what could be causing it to stay in memory?</p> <p>Thanks!</p> <p>Update: I am using the following code to create the window:</p> <pre><code> Public Sub OpenPageWindow(ByVal nick As String) Dim found As Boolean For Each pw As PageWindow In Application.Current.Windows.OfType(Of PageWindow)() If Not pw.Tag Is Nothing Then If pw.Tag.ToString() = nick Then If pw.IsVisible = False Then pw.Show() End If Exit Sub End If End If Next If found = False Then Dim p As New PageWindow With {.Name = "pw" &amp; nick, .Tag = nick, _ .Title = "Chatting with " &amp; nick, .ShowActivated = False} p.Show() End If End Sub </code></pre> <p>and the following code-behind for the window itself:</p> <pre><code>Public Class PageWindow Implements System.IDisposable Public UserPressedExit As Boolean Dim MainWin As MainWindow Private _IsBuddy As Boolean Private _IsBlocked As Boolean Private _IsOnline As Boolean Public Property FirstOpen As Boolean Public Property IsOnline As Boolean Get Return _IsOnline End Get Set(ByVal value As Boolean) If _IsOnline &lt;&gt; value Then _IsOnline = value End If End Set End Property Public Property IsBuddy As Boolean Get Return _IsBuddy End Get Set(ByVal value As Boolean) If _IsBuddy &lt;&gt; value Then _IsBuddy = value 'If value Then ' btnAddBuddy.IsEnabled = False ' iDeleteBuddy.IsEnabled = True 'Else ' btnAddBuddy.IsEnabled = True ' iDeleteBuddy.IsEnabled = False 'End If End If End Set End Property Private Sub Window_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Try MainWin = CType(Application.Current.Windows(0), MainWindow) Catch ex As Exception txtChat.AppendText("ERROR: " &amp; ex.Message.ToString() &amp; Environment.NewLine) End Try End Sub Private Sub Window_Closed(ByVal sender As System.Object, ByVal e As System.EventArgs) Me.Dispose() End Sub Private Sub txtTitle_PreviewMouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Input.MouseButtonEventArgs) Me.DragMove() End Sub Private Sub pbMin_PreviewMouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Input.MouseButtonEventArgs) WindowState = Windows.WindowState.Minimized End Sub Private Sub pbClose_PreviewMouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Input.MouseButtonEventArgs) UserPressedExit = True txtChat.Clear() Me.Close() End Sub Private Sub txtSend_KeyUp(ByVal sender As System.Object, ByVal e As System.Windows.Input.KeyEventArgs) If e.Key = Key.Enter Then btnSend_Click(Nothing, Nothing) End If End Sub Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) If String.IsNullOrWhiteSpace(txtSend.Text) Then MessageBox.Show("Empty Message.") txtSend.Text = "" Else Send(txtSend.Text) txtSend.Text = "" End If End Sub Public Sub Page(ByVal message As String) If String.IsNullOrEmpty(message) = False Then If MainWin.Settings.ShowPageTimestamp Then txtChat.AppendText(Me.Tag.ToString() &amp; " (" &amp; Format(Date.Now, "HH:mm:ss") &amp; "): " &amp; message &amp; Environment.NewLine) Else txtChat.AppendText(Me.Tag.ToString() &amp; ": " &amp; message &amp; Environment.NewLine) End If If Me.IsFocused = False Then End If End If End Sub Private Sub Send(ByVal text As String) 'work on 389 event MainWin.Send("PAGE " + Me.Tag.ToString + " " + text) If MainWin.Settings.ShowPageTimestamp Then txtChat.AppendText(MainWin.Settings.Nick &amp; ": " &amp; text &amp; Environment.NewLine) Else txtChat.AppendText(MainWin.Settings.Nick &amp; " (" &amp; Format(Date.Now, "HH:mm:ss") &amp; "): " &amp; text &amp; Environment.NewLine) End If End Sub #Region "IDisposable Support" Private disposed As Boolean ' To detect redundant calls ' This code added by Visual Basic to correctly implement the disposable pattern. Public Sub Dispose() Implements IDisposable.Dispose ' Do not change this code. Put cleanup code in Dispose(ByVal disposing As Boolean) above. Dispose(True) GC.SuppressFinalize(Me) End Sub Protected Overridable Sub Dispose(ByVal disposing As Boolean) If Not Me.disposed Then If disposing Then 'dispose managed state (managed objects). RemoveHandler Me.Loaded, AddressOf Window_Loaded RemoveHandler txtTitle.PreviewMouseDown, AddressOf txtTitle_PreviewMouseDown RemoveHandler pbMin.PreviewMouseUp, AddressOf pbMin_PreviewMouseUp RemoveHandler pbClose.PreviewMouseUp, AddressOf pbClose_PreviewMouseUp RemoveHandler txtSend.KeyUp, AddressOf txtSend_KeyUp RemoveHandler btnSend.Click, AddressOf btnSend_Click RemoveHandler Me.Closed, AddressOf Window_Closed BindingOperations.ClearBinding(txtTitle, TextBlock.TextProperty) Me.Icon = Nothing ibBackground = Nothing 'iDeleteBuddy = Nothing 'btnAddBuddy = Nothing 'iBlockUser = Nothing pbMin = Nothing pbClose = Nothing MainWin = Nothing Me.Resources.Clear() GC.Collect() End If End If Me.disposed = True End Sub Protected Overrides Sub Finalize() ' Do not change this code. Put cleanup code in Dispose(ByVal disposing As Boolean) above. Dispose(False) MyBase.Finalize() End Sub #End Region End Class </code></pre> <p>Then the following code for the XAML:</p> <pre><code>&lt;Window x:Class="PageWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="PageWindow" Height="300" Width="400" Icon="Images\R2.ico" WindowStyle="None" ResizeMode="NoResize" WindowStartupLocation="Manual" Loaded="Window_Loaded" BorderThickness="2" BorderBrush="#FFAE28" Closed="Window_Closed"&gt; &lt;Window.Resources&gt; &lt;Style x:Key="sRenegadeButton" TargetType="Button"&gt; &lt;Setter Property="FontFamily" Value="Franklin Gothic Medium" /&gt; &lt;Setter Property="FontSize" Value="12px" /&gt; &lt;Setter Property="FontWeight" Value="Bold" /&gt; &lt;Setter Property="Background" Value="Transparent" /&gt; &lt;Setter Property="Foreground" Value="#FFAE28" /&gt; &lt;Setter Property="Template"&gt; &lt;Setter.Value&gt; &lt;ControlTemplate TargetType="Button"&gt; &lt;Border Name="Border" BorderBrush="#FFAE28" BorderThickness="1" CornerRadius="3" Background="{TemplateBinding Background}" SnapsToDevicePixels="True"&gt; &lt;Border Name="innerborder" BorderBrush="#FFAE28" BorderThickness="1" CornerRadius="3" Background="{TemplateBinding Background}" SnapsToDevicePixels="True" Margin="1"&gt; &lt;ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" Name="content" /&gt; &lt;/Border&gt; &lt;/Border&gt; &lt;ControlTemplate.Triggers&gt; &lt;Trigger Property="IsMouseOver" Value="True"&gt; &lt;Setter TargetName="Border" Property="BorderBrush" Value="#FFD528" /&gt; &lt;Setter TargetName="innerborder" Property="BorderBrush" Value="#FFD528" /&gt; &lt;Setter Property="Background" Value="#28FFAE28" /&gt; &lt;Setter Property="Foreground" Value="#FFD528" /&gt; &lt;/Trigger&gt; &lt;Trigger Property="IsPressed" Value="True"&gt; &lt;Setter Property="Background" Value="#28FFAE28" /&gt; &lt;Setter TargetName="content" Property="RenderTransform" &gt; &lt;Setter.Value&gt; &lt;TranslateTransform Y="1.0" /&gt; &lt;/Setter.Value&gt; &lt;/Setter&gt; &lt;/Trigger&gt; &lt;Trigger Property="IsEnabled" Value="False"&gt; &lt;Setter TargetName="Border" Property="BorderBrush" Value="#80E6A023" /&gt; &lt;Setter TargetName="innerborder" Property="BorderBrush" Value="#80E6A023" /&gt; &lt;Setter Property="Foreground" Value="#8CFFD528" /&gt; &lt;/Trigger&gt; &lt;/ControlTemplate.Triggers&gt; &lt;/ControlTemplate&gt; &lt;/Setter.Value&gt; &lt;/Setter&gt; &lt;/Style&gt; &lt;Style x:Key="sRenegadeTextBox" TargetType="TextBox"&gt; &lt;Setter Property="FontFamily" Value="Franklin Gothic Medium" /&gt; &lt;Setter Property="FontSize" Value="12px" /&gt; &lt;Setter Property="Foreground" Value="#FFD528" /&gt; &lt;Setter Property="Background" Value="#28FFAE28" /&gt; &lt;Setter Property="BorderBrush" Value="#FFAE28" /&gt; &lt;Setter Property="CaretBrush" Value="#FFAE28" /&gt; &lt;Setter Property="SelectionBrush" Value="#FFD528" /&gt; &lt;Setter Property="BorderThickness" Value="1" /&gt; &lt;Setter Property="Template"&gt; &lt;Setter.Value&gt; &lt;ControlTemplate TargetType="TextBox"&gt; &lt;Border Name="Bd" BorderThickness="{TemplateBinding BorderThickness}" BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}" SnapsToDevicePixels="true"&gt; &lt;ScrollViewer Name="PART_ContentHost" Background="{TemplateBinding Background}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" /&gt; &lt;/Border&gt; &lt;ControlTemplate.Triggers&gt; &lt;Trigger Property="IsEnabled" Value="False"&gt; &lt;Setter Property="Foreground" Value="#8CFFD528" /&gt; &lt;Setter Property="Background" Value="#1EFFAE28" /&gt; &lt;Setter Property="BorderBrush" Value="#80E6A023" /&gt; &lt;Setter TargetName="PART_ContentHost" Property="Background" Value="#1EFFAE28"/&gt; &lt;/Trigger&gt; &lt;/ControlTemplate.Triggers&gt; &lt;/ControlTemplate&gt; &lt;/Setter.Value&gt; &lt;/Setter&gt; &lt;/Style&gt; &lt;/Window.Resources&gt; &lt;Grid&gt; &lt;Grid.RowDefinitions&gt; &lt;RowDefinition Height="21" /&gt; &lt;RowDefinition Height="32" /&gt; &lt;RowDefinition Height="*" /&gt; &lt;RowDefinition Height="27" /&gt; &lt;/Grid.RowDefinitions&gt; &lt;Grid.Background&gt; &lt;ImageBrush x:Name="ibBackground" ImageSource="Images\page_back.gif" /&gt; &lt;/Grid.Background&gt; &lt;Border Grid.Row="0" BorderBrush="#FFAE28" BorderThickness="0,0,0,2" SnapsToDevicePixels="True" /&gt; &lt;Grid Name="gTitleBar" Grid.Row="0" Background="#32FFAE28"&gt; &lt;Grid.ColumnDefinitions&gt; &lt;ColumnDefinition Width="*" /&gt; &lt;ColumnDefinition Width="30" /&gt; &lt;ColumnDefinition Width="32" /&gt; &lt;/Grid.ColumnDefinitions&gt; &lt;TextBlock Name="txtTitle" Grid.Column="0" Text="{Binding Title,RelativeSource={RelativeSource FindAncestor,AncestorType=Window}}" HorizontalAlignment="Stretch" VerticalAlignment="Center" TextAlignment="Left" Margin="4,0,1,0" PreviewMouseDown="txtTitle_PreviewMouseDown" Foreground="#FED528" FontFamily="Franklin Gothic Medium" /&gt; &lt;Image Name="pbMin" Grid.Column="1" Width="28" Height="15" HorizontalAlignment="Center" VerticalAlignment="Center" PreviewMouseUp="pbMin_PreviewMouseUp" Stretch="None"&gt; &lt;Image.Style&gt; &lt;Style TargetType="{x:Type Image}"&gt; &lt;Setter Property="Source" Value="Images\min.png" /&gt; &lt;Style.Triggers&gt; &lt;Trigger Property="IsMouseOver" Value="True"&gt; &lt;Setter Property="Source" Value="Images\min_o.png" /&gt; &lt;/Trigger&gt; &lt;/Style.Triggers&gt; &lt;/Style&gt; &lt;/Image.Style&gt; &lt;/Image&gt; &lt;Image Name="pbClose" Grid.Column="2" Width="28" Height="15" HorizontalAlignment="Left" VerticalAlignment="Center" PreviewMouseUp="pbClose_PreviewMouseUp" Stretch="None"&gt; &lt;Image.Style&gt; &lt;Style TargetType="{x:Type Image}"&gt; &lt;Setter Property="Source" Value="Images\close2.png" /&gt; &lt;Style.Triggers&gt; &lt;Trigger Property="IsMouseOver" Value="True"&gt; &lt;Setter Property="Source" Value="Images\close2_o.png" /&gt; &lt;/Trigger&gt; &lt;/Style.Triggers&gt; &lt;/Style&gt; &lt;/Image.Style&gt; &lt;/Image&gt; &lt;/Grid&gt; &lt;Grid Grid.Row="1"&gt; &lt;Grid.ColumnDefinitions&gt; &lt;ColumnDefinition Width="*" /&gt; &lt;ColumnDefinition Width="25" /&gt; &lt;ColumnDefinition Width="25" /&gt; &lt;ColumnDefinition Width="25" /&gt; &lt;ColumnDefinition Width="*" /&gt; &lt;/Grid.ColumnDefinitions&gt; &lt;!--&lt;Button Name="btnAddBuddy" Grid.Column="1" Margin="0,2,0,0"&gt; &lt;Button.Style&gt; &lt;Style TargetType="{x:Type Button}"&gt; &lt;Setter Property="Background" Value="Transparent" /&gt; &lt;Setter Property="Template"&gt; &lt;Setter.Value&gt; &lt;ControlTemplate TargetType="{x:Type Button}"&gt; &lt;Grid&gt; &lt;Border Name="Border" BorderBrush="Transparent" BorderThickness="1" CornerRadius="3" Background="{TemplateBinding Background}" SnapsToDevicePixels="True"&gt; &lt;/Border&gt; &lt;Image Name="iAddBuddy" Margin="1" Source="Images\Add Buddy 32x36.png" /&gt; &lt;Image Name="iAddBuddyDissabled" Margin="1" Source="Images\Add Buddy 32x36.png" Visibility="Hidden" /&gt; &lt;/Grid&gt; &lt;ControlTemplate.Triggers&gt; &lt;Trigger Property="IsMouseOver" Value="True"&gt; &lt;Setter TargetName="Border" Property="BorderBrush" Value="#FFD528" /&gt; &lt;/Trigger&gt; &lt;Trigger Property="IsPressed" Value="True"&gt; &lt;Setter Property="Background" Value="#FFD528" /&gt; &lt;/Trigger&gt; &lt;Trigger Property="IsEnabled" Value="False"&gt; &lt;Setter TargetName="Border" Property="BorderBrush" Value="Transparent" /&gt; &lt;Setter TargetName="iAddBuddy" Property="Visibility" Value="Hidden" /&gt; &lt;Setter TargetName="iAddBuddyDissabled" Property="Visibility" Value="Visible" /&gt; &lt;/Trigger&gt; &lt;/ControlTemplate.Triggers&gt; &lt;/ControlTemplate&gt; &lt;/Setter.Value&gt; &lt;/Setter&gt; &lt;/Style&gt; &lt;/Button.Style&gt; &lt;/Button&gt; &lt;Border Name="bDeleteBuddyBorder" Grid.Column="2" BorderThickness="1" SnapsToDevicePixels="True" Margin="0,2,0,0"&gt; &lt;Image Name="iDeleteBuddy" Margin="1" IsEnabled="True" Source="Images\Delete 32x32.png"&gt; &lt;Image.Style&gt; &lt;Style TargetType="{x:Type Image}"&gt; &lt;Style.Triggers&gt; &lt;Trigger Property="IsEnabled" Value="False"&gt; &lt;Setter Property="Source" Value="Images\Delete Disabled 32x32.png" /&gt; &lt;/Trigger&gt; &lt;/Style.Triggers&gt; &lt;/Style&gt; &lt;/Image.Style&gt; &lt;/Image&gt; &lt;Border.Style&gt; &lt;Style TargetType="Border"&gt; &lt;Setter Property="BorderBrush" Value="Transparent" /&gt; &lt;Setter Property="Background" Value="Transparent" /&gt; &lt;Style.Triggers&gt; &lt;Trigger Property="IsMouseOver" Value="True"&gt; &lt;Setter Property="BorderBrush" Value="#FFD528" /&gt; &lt;/Trigger&gt; &lt;/Style.Triggers&gt; &lt;/Style&gt; &lt;/Border.Style&gt; &lt;/Border&gt; &lt;Border Name="bBlockUserBorder" Grid.Column="3" BorderThickness="1" SnapsToDevicePixels="True" Margin="0,2,0,0"&gt; &lt;Image Name="iBlockUser" Margin="1" IsEnabled="True" Source="Images\Block Buddy 32x36.png"&gt; &lt;Image.Style&gt; &lt;Style TargetType="{x:Type Image}"&gt; &lt;Style.Triggers&gt; &lt;Trigger Property="IsEnabled" Value="False"&gt; &lt;Setter Property="Source" Value="Images\Block Buddy Dissabled 32x36.png" /&gt; &lt;/Trigger&gt; &lt;/Style.Triggers&gt; &lt;/Style&gt; &lt;/Image.Style&gt; &lt;/Image&gt; &lt;Border.Style&gt; &lt;Style TargetType="Border"&gt; &lt;Setter Property="BorderBrush" Value="Transparent" /&gt; &lt;Setter Property="Background" Value="Transparent" /&gt; &lt;Style.Triggers&gt; &lt;Trigger Property="IsMouseOver" Value="True"&gt; &lt;Setter Property="BorderBrush" Value="#FFD528" /&gt; &lt;/Trigger&gt; &lt;/Style.Triggers&gt; &lt;/Style&gt; &lt;/Border.Style&gt; &lt;/Border&gt;--&gt; &lt;/Grid&gt; &lt;TextBox Name="txtChat" Grid.Row="2" Margin="4,2,4,2" Style="{StaticResource sRenegadeTextBox}" UndoLimit="0" /&gt; &lt;Grid Grid.Row="3"&gt; &lt;Grid.ColumnDefinitions&gt; &lt;ColumnDefinition Width="*" /&gt; &lt;ColumnDefinition Width="45" /&gt; &lt;/Grid.ColumnDefinitions&gt; &lt;TextBox Name="txtSend" Grid.Column="0" MaxLength="495" Height="22" Margin="4,1,2,4" VerticalContentAlignment="Center" Style="{StaticResource sRenegadeTextBox}" UndoLimit="0" KeyUp="txtSend_KeyUp" /&gt; &lt;Button Name="btnSend" Content="Send" Grid.Column="1" Style="{StaticResource sRenegadeButton}" Height="22" Width="40" Margin="0,1,4,4" Click="btnSend_Click" /&gt; &lt;/Grid&gt; &lt;/Grid&gt; &lt;/Window&gt; </code></pre> <p>Basically even if I create an empty WPF Window with a background image for the main grid and 2 eventhandlers Opening and Closed it still stays in memory, but when all the 'empty wpf' windows are closed the memory only decreases a little. just a little. Why arent simple windows disposed of entirely? I have even tried implementing the disposed functionality coded above.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload