Memory leak in storyboard under Win8 only?

2

I have made an app, which has a slideshow in it. i run this app on 5 PCs. 3 Win7 and 2 Win8 (all x64, my app is x86). On the Win7-PC's I have no problems. Under Win8 I get (after a not predictable time) some error messages. An OutOfMemory-exception and an UCEERR_RENDERTHREADFAILURE (HRESULT: 0x88980406). Microsoft says this about it:

  1. If System.OutOfMemoryExceptions are being reported, then monitor the process's memory usage in Performance Monitor; particularly the Process\Virtual Bytes, Process\Private Bytes, and .NET CLR Memory# Bytes in All Heaps counters. Also monitor the User Objects and GDI Objects for the process in Windows Task Manager. If you can determine that a specific resource is being exhausted, then troubleshoot the application to fix whatever is causing that resource consumption. Ultimately that should resolve the System.OutOfMemoryException.

I cannot wait 1 to 5 days only monitoring the system and waiting for the error to happen. But maybe someone of you can see the error in my code?

Here is my XAML-Code:

    <Window.Resources>
    <Style x:Key="ImgZIndexStyle" TargetType="{x:Type Image}">
        <Setter Property="Panel.ZIndex" Value="2"/>
        <Style.Triggers>
            <Trigger Property="Image.Opacity" Value="1">
                <Setter Property="Panel.ZIndex" Value="3"/>
            </Trigger>
        </Style.Triggers>
    </Style>
    <Storyboard x:Key="FaderStoryboardHide1Show2">
        <DoubleAnimation Storyboard.TargetName="FirstImage" 
         Storyboard.TargetProperty="Opacity"
         To="0" Duration="0:00:01" />
        <DoubleAnimation Storyboard.TargetName="SecondImage" 
         Storyboard.TargetProperty="Opacity"
         To="1" Duration="0:00:01" />
    </Storyboard>
    <Storyboard x:Key="FaderStoryboardHide2Show1">
        <DoubleAnimation Storyboard.TargetName="SecondImage" 
         Storyboard.TargetProperty="Opacity"
         To="0" Duration="0:00:01" />
        <DoubleAnimation Storyboard.TargetName="FirstImage" 
         Storyboard.TargetProperty="Opacity"
         To="1" Duration="0:00:01" />
    </Storyboard>
</Window.Resources>
<Grid>
    <Grid Name="SlideShowGrid" ZIndex="0">
        <Button Name="SlideShowButton" Click="SlideShowButton_OnClick">
            <Grid>
                <Image x:Name="FirstImage" Stretch="UniformToFill" Style="{StaticResource ImgZIndexStyle}" Opacity="1" />
                <Image x:Name="SecondImage" Stretch="UniformToFill" Style="{StaticResource ImgZIndexStyle}" Opacity="0" />
            </Grid>
        </Button>
    </Grid>
</Grid>

And my Class:

private void SlideShowTimer_Tick(object sender, EventArgs e)
{
    Image newImage;
    Storyboard tempStoryboard;
    if (FirstImage.Opacity == 1)
    {
        tempStoryboard = (Storyboard)FindResource("FaderStoryboardHide1Show2");
        newImage = SecondImage;
    }
    else
    {
        tempStoryboard = (Storyboard)FindResource("FaderStoryboardHide2Show1");
        newImage = FirstImage;
    }
    if (_imageCounter >= _fileList.Count - 1)
        _imageCounter = 0;
    else
        _imageCounter++;
    newImage.Source = new BitmapImage(new Uri(_fileList[_imageCounter]));
    tempStoryboard.Begin();
}
c#
.net
wpf
memory-management
memory-leaks
asked on Stack Overflow Aug 18, 2015 by Marcel GrĂ¼ger • edited Aug 21, 2015 by CharithJ

1 Answer

0

Garbage collector uses heuristic algorithms to optimize certain operations. So, you cannot expect consistent behaviour for your memory issues in different PCs.

Don't let your disposable objects to go out of scope without disposing. You should dispose your existing image before you create a new instance.

if (newImage != null)
    newImage.Dispose();

newImage.Source = new BitmapImage(new Uri(_fileList[_imageCounter]));

Edit

The Bitmap class is an implementation of the Image class. The Image class is an abstract class. So, use Bitmap which is disposable instead of Image.

Bitmap newImage;
....
mewImage.Dispose();
answered on Stack Overflow Aug 19, 2015 by CharithJ • edited Aug 21, 2015 by CharithJ

User contributions licensed under CC BY-SA 3.0