【问题标题】:Memory Leak in WPFWPF 中的内存泄漏
【发布时间】:2009-08-13 12:52:16
【问题描述】:

我编写了一个非常简单的新手应用程序,它有一个 6 面多面体(一个“盒子”),当我单击一个按钮时它会旋转 180 度。然后在下一次单击时再次旋转回来。每次旋转都会再占用 90MB,直到我关闭应用程序才松开。该框在 XAML 中定义。 StoryboardDoubleAnimationPropertyPath 等都是在构造函数中创建一次。按钮代码如下所示:

    private void button_Storyboard1_Click(object sender, RoutedEventArgs e)
    {
        GC.Collect();

        if (_bFront)
        {
            _myDoubleAnimation.From = 0;
            _myDoubleAnimation.To = 180;
            _bFront = false;
        }
        else
        {
            _myDoubleAnimation.From = 180;
            _myDoubleAnimation.To = 0;
            _bFront = true;
        }
        _myDoubleAnimation.Duration = _Duration;
        Storyboard.SetTargetName(_myDoubleAnimation, "rotate_me");
        Storyboard.SetTargetProperty(_myDoubleAnimation, _PropP);
        _sb.Children.Add(_myDoubleAnimation);
        _sb.Begin(this.viewport3D1);
    }

转了几圈后我就没有记忆了!怎么回事?

【问题讨论】:

  • 请告诉我这是什么gfx卡?

标签: wpf memory-leaks 3d


【解决方案1】:

这里可能完全错了,但是您不是在每次点击时都将 _myDoubleAnimation 添加到 _sb.Children 中,而不仅仅是更新它吗?

【讨论】:

  • 我昨晚想到了这个并尝试做 _sb.Children.Clear();在 Add() 之前没有效果。我没有看到名为“更新”的方法 - 你是什么意思? (对不起,我是 wpf 新手) gfx 卡是 ATi Radeon X300,它说它支持 DirectX 9.0
  • 啊-哈哈!但无论如何你都是对的!当我将 Add to the C'Tor(所以我只添加一次)移动到 Animation(To... 和 From...)时,仍然可以识别,但不会再出现内存泄漏!谢谢!!!我仍然不明白为什么 _sb.Children.Clear() 没有帮助。
  • 不,听起来很奇怪 :) 我希望 _sb.Children.Clear() 也能修复泄漏 :)
猜你喜欢
  • 2011-02-21
  • 2010-09-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-05
  • 2011-03-19
相关资源
最近更新 更多