Rafa S.
Rafa S.

Reputation: 35

Has this code a memory leak?

I think I have a memory leak in my WPF code, and I'm doing tests. The part I'm not sure if it has problems is this one:

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;
}

I use these two functions to create anymations on the fly, but I'm not sure if doing this way is creating Memory Leaks.

Can someone tell me if this code has leaks or not? I was suspecting on the EventHandlers "completed" and also adding the storyboard to the windows.Resources.

Upvotes: 0

Views: 335

Answers (1)

Rafa S.
Rafa S.

Reputation: 35

I fixed this. It seems it was a problem with .NET Framework 3.5

I added this:

new HwndSource(new HwndSourceParameters());

to App.xaml constructor and the problem is gone.

Upvotes: 1

Related Questions