gpt4 book ai didi

wpf - 对应用程序级资源使用 DynamicResource 可能会导致泄漏

转载 作者:行者123 更新时间:2023-12-02 05:16:02 24 4
gpt4 key购买 nike

似乎使用 DynamicResource 引用应用程序级资源可能会导致发生内存泄漏。

请参阅此 WPF 论坛 post了解更多信息、如何重现它以及一些解决方法。

我的问题是:还有其他人遇到过吗?如果是这样,您是如何解决这个问题的?

顺便说一句,似乎有很多情况不会发生这种泄漏,也许最好的问题是:到底什么情况会发生这种泄漏,什么情况不会发生?

为了方便起见,这里是重现它的代码:

App.xaml

<Application
x:Class="WeakReferences.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="Window1.xaml"
>
<Application.Resources>
<SolidColorBrush x:Key="MyBrush" Color="SkyBlue"/>
</Application.Resources>
</Application>

Window1.xaml

<Window
x:Class="WeakReferences.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1"
Height="300"
Width="300"
>
<Grid>
<Button
Name="ReleaseButton"
Content="Release Reference"
Click="Button_Click"
/>
</Grid>
</Window>

Window1.xaml.cs

public partial class Window1 : Window
{
object p;

public Window1()
{
InitializeComponent();

p = new Page1();
}

private void Button_Click(object sender, RoutedEventArgs e)
{
p = null;

GC.Collect();
}
}

Page1.xaml

<Page
x:Class="WeakReferences.Page1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Page1"
Background="{DynamicResource MyBrush}"
>
<Grid>
</Grid>
</Page>

Page1.xaml.cs

public partial class Page1 : Page
{
public Page1()
{
InitializeComponent();
}

~Page1()
{
Trace.TraceInformation("Page1 Finalized.");
}
}

最佳答案

微软有confirmed这是一个错误,已在 .NET 4.0 中修复。

据我所知,只有当使用 DynamicResource 引用应用程序级资源的对象从未成为可视化树的一部分时,此错误才会重现。希望看到一些反证据……或者进一步澄清泄漏何时确实发生。

更新:此错误也在 .NET 3.5 中得到修复。看到这个热点fix了解更多信息。

关于wpf - 对应用程序级资源使用 DynamicResource 可能会导致泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/628386/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com