- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
使用更新的 .NET 4.0,我发现了一个奇怪的内存泄漏,可以通过以下示例代码重现。
<DropShadowEffect>
谁的Color
依赖属性绑定(bind)到 App 对象中的属性。DropShadowEffect
时才会发生资源。不会发生在 SolidColorBrush
上谁的Color
也绑定(bind)到相同的来源。如果有人能告诉我为什么这个泄漏发生在 DropShadowEffect
上,我将不胜感激。但不在 SolidColorBrush
上?
App.xml
<Application x:Class="WpfSimple.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Application.Resources>
<!--this one make GC unable to collect LeakWindow-->
<DropShadowEffect x:Key="AppDropShadowColor"
Color="{Binding Source={x:Static Application.Current}, Path=DropShadowColor, Mode=OneWay}" />
<!--this one does not leak-->
<SolidColorBrush x:Key="AppBackground"
Color="{Binding Source={x:Static Application.Current}, Path=DropShadowColor, Mode=OneWay}" />
</Application.Resources>
</Application>
App.xml.cs 启动 MainWindow 并实现 INotifyPropertyChanged
为属性(property)DropShadowColor
.
public partial class App : Application, INotifyPropertyChanged
{
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
// start main window
var mainWindow = new MainWindow();
mainWindow.Show();
}
private Color _dropShadowColor = Colors.Blue;
public Color DropShadowColor
{
get { return _dropShadowColor; }
set {
_dropShadowColor = value;
OnPropertyChanged("DropShadowColor");
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) { handler(this, new PropertyChangedEventArgs(propertyName)); }
}
}
MainWindow.xml 和 MainWindow.xml.cs 有一个创建 LeakWindow
的按钮,如下所示。
var win = new LeakWindow {Owner = this};
win.Show();
还有另一个按钮可以做 GC.Collect()
;
LeakWindow.xml
<Window x:Class="WpfSimple.LeakWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Leak" Height="300" Width="300">
<Grid>
<!--leak-->
<Border Width="200" Height="200" BorderThickness="1" BorderBrush="Black" Effect="{StaticResource AppDropShadowColor}"/>
<!--no leak if comment out above and uncomment below-->
<!--<Border Width="200" Height="200" BorderThickness="1" BorderBrush="Black" Background="{StaticResource AppBackground}"/>-->
</Grid>
</Window>
LeakWindow.xml.cs
public partial class LeakWindow : Window
{
public LeakWindow()
{
InitializeComponent();
}
~LeakWindow()
{
Debug.WriteLine("LeakWindow finalized");
}
}
更新
OneTime
也无济于事。DynamicResource
没有帮助。进一步调查显示泄漏是由 EventHandler
引起的引用自 DropShadowEffect
至 Border.Effect
.可能是由于 DropShadowEffect
中的绑定(bind)而导致的更改通知.
不过,奇怪的是 为什么这只发生在 Border.Effect
上但不在Border.Background
?
工作场所
添加x:Shared=false
至 <DropShadowEffect>
在 app.xml 中可以解决这个问题。我现在可以拥有应用程序范围内定义的资源,但会降低内存效率。
最佳答案
我认为问题是由 DropShadowEffect
附加到可视化树的方式引起的。将 DropShadowEffect
移动到您的控件模板中而不是将其作为资源可能会解决泄漏问题,但是您将失去共享资源...
关于c# - Border.Effect 绑定(bind)泄漏内存但 Border.Background 没有,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31735010/
有没有办法像 super 速记样式一样在 CSS 中组合 border-top,border-right,border-left,border-bottom。 例如: border: (1px sol
这个问题在这里已经有了答案: Combining border-top,border-right,border-left,border-bottom in CSS (4 个答案) 关闭 3 年前。
所以,我是那些以始终使用最新版本的浏览器而自豪的人之一(当然 Internet Explorer 除外 - 我说的不是那个浏览器)。 我遇到了 this awesome CSS3 website详细介
border-top-color 是 #9b9c9d 而 border-bottom-color 是 #f6f9fc .渐变旨在在border-left 和border-right 上将顶部颜色过渡到
我将开始开发一个新网站,并准备处理浏览器用于计算元素宽度和高度的不同方法 (box model stuff)。不知何故,我想到了:如果我只是将 box-sizing 应用于网站中的所有元素会怎么样?
这是 fiddle :http://jsfiddle.net/3Ys2d/ CSS div{ border: solid 3px blue; border-left-color: re
这个接缝很容易,但我还没有找到任何方法来做到这一点。我有 3 个 div(但解决方案必须适用于 n 个 div),如下图所示: |分区 1 ||分区 2 ||第 3 部分 | 例如: 我想要这样的普通
由于视网膜显示器具有如此高的像素密度,因此在这些显示器上 1 像素的边框看起来非常大。正如 Brad Birdsall 所建议的,CSS box-shadow属性可用于创建“0,5 px”边框,在
我可以这样写边框的样式: border: 2px solid #DDDDDD; 或者像这样: -moz-border-bottom-colors: none; -moz-border-left-col
当在已经有 1px 边框的 div 中覆盖 border-bottom 的大小时,Firefox 和 Chrome 呈现 border-left 和 border-right 不正确: HTML C
我是CSS3新手,最近在学习border-image属性,在看W3C文档的时候: http://dev.w3.org/csswg/css-backgrounds/#border-image-width
我正在尝试在使用 border-radius 属性的 div 上使用 border 属性。 这是我的 CSS: #page { border: 1px solid #beb2b2; w
我只想在已经具有顶部和底部边框(1px 纯灰色)的 div 上添加一个左边框(5px 纯蓝色)。我希望 border-left 位于 border-top 和 border-bottom 之上,但浏览
我有一个使用 border-radius 的带有圆 Angular 的流体宽度 div,以及 div 一侧的大边框,颜色不同。 当浏览器窗口足够小时,所有边框都会正常运行。但是,当我放大窗口大小时,我
导致“错误”的 Css: div { width: 100px; height: 100px; background-color: transparent; box-s
Border-bottom长度小于border-right怎么办? img { border-bottom: 5px solid #02a8d8; border-right:1px s
我是一名优秀的程序员,十分优秀!