这段代码有内存泄漏吗?

时间:2015-10-27 18:27:57

标签: c# wpf memory-leaks

我认为我的WPF代码中存在内存泄漏,而且我正在进行测试。我不确定它是否有问题的部分就是这个:

public void MakeFadeIn(Window window, FrameworkElement target,  MakeFadeCallBack makeFadeCallBack, double from, double to)
{
    DoubleAnimation anim1 = new DoubleAnimation(from, to, TimeSpan.FromSeconds(0.2)) {BeginTime = TimeSpan.FromSeconds(0)};
    Storyboard storyBoard = new Storyboard();

    string storyBoardName = "storyBoardMakeFadeIn" + CreateRandomNum();
    Storyboard.SetTarget(anim1, target);
    Storyboard.SetTargetProperty(anim1, new PropertyPath("(Opacity)"));
    storyBoard.Children.Add(anim1);

    _eventHandler = (sndr, evtArgs) =>
    {
        window.Resources.Remove(storyBoardName);
        storyBoard.Completed -= _eventHandler;          
        if (makeFadeCallBack != null)
            makeFadeCallBack();
    };

    storyBoard.Completed += _eventHandler;                
    window.Resources.Add(storyBoardName, storyBoard);

    storyBoard.Begin();
}

public Storyboard CreateAnimationStoryBoard(Window window, Grid gridContent, FrameworkElement startElement, Image imageOrig, BitmapImage bitmapImageOrig, double endPointX, double endPointY, double toScale)
{
    GeneralTransform gt = startElement.TransformToAncestor(window);
    Point rootPoint = gt.Transform(new Point(0, 0));
    Image image = new Image
    {
        Source = bitmapImageOrig,
        Opacity = 0.8,
        Height = imageOrig.DesiredSize.Height,
        Width = imageOrig.DesiredSize.Width,
        Stretch = Stretch.UniformToFill,
        Margin = new Thickness(rootPoint.X, rootPoint.Y, 0, 0),
        HorizontalAlignment = HorizontalAlignment.Left,
        VerticalAlignment = VerticalAlignment.Top,
        RenderTransformOrigin = new Point(0.5, 0.5)
    };
    gridContent.Children.Add(image);

    TransformGroup transGroup = new TransformGroup();

    ScaleTransform scale = new ScaleTransform();
    TranslateTransform trans = new TranslateTransform();
    transGroup.Children.Add(scale);
    transGroup.Children.Add(trans); // BE CAREFUL, order is important, Scale ALWAYS before trans

    image.RenderTransform = transGroup;
    window.RegisterName("translateArticleToOrder", transGroup);

    DoubleAnimation anim1 = new DoubleAnimation(endPointX - rootPoint.X, TimeSpan.FromSeconds(0.4));
    DoubleAnimation anim2 = new DoubleAnimation(endPointY - rootPoint.Y - image.Height, TimeSpan.FromSeconds(0.4));
    DoubleAnimation animScaleX = new DoubleAnimation(toScale, TimeSpan.FromSeconds(0.4));
    DoubleAnimation animScaleY = new DoubleAnimation(toScale, TimeSpan.FromSeconds(0.4));

    Storyboard storyBoard = new Storyboard();
    window.Resources.Add("storyBoardArticleToOrder", storyBoard);
    Storyboard.SetTargetName(storyBoard, "translateArticleToOrder");
    Storyboard.SetTargetProperty(anim1, new PropertyPath("Children[1].X"));
    Storyboard.SetTargetProperty(anim2, new PropertyPath("Children[1].Y"));
    Storyboard.SetTargetProperty(animScaleX, new PropertyPath("Children[0].ScaleX"));
    Storyboard.SetTargetProperty(animScaleY, new PropertyPath("Children[0].ScaleY"));

    storyBoard.Children.Add(anim1);
    storyBoard.Children.Add(anim2);
    storyBoard.Children.Add(animScaleX);
    storyBoard.Children.Add(animScaleY);

    _eventHandler = (sndr, evtArgs) =>
    {
        storyBoard.Completed -= _eventHandler;
        window.Resources.Remove("storyBoardArticleToOrder");
        gridContent.Children.Remove(image);
        window.UnregisterName("translateArticleToOrder");                
    };
    storyBoard.Completed += _eventHandler;
    return storyBoard;
}

我使用这两个函数动态创建任何图像,但我不确定这样做是否会造成内存泄漏。

有人可以告诉我此代码是否有泄漏?我怀疑EventHandlers“已完成”并且还将故事板添加到windows.Resources。

1 个答案:

答案 0 :(得分:1)

我修好了。这似乎是.NET Framework 3.5的一个问题

我补充说:

new HwndSource(new HwndSourceParameters());

到App.xaml构造函数,问题就消失了。