Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在Windows Phone 7中更改图像时内存泄漏_C#_Silverlight_Windows Phone 7_Memory Leaks - Fatal编程技术网

C# 在Windows Phone 7中更改图像时内存泄漏

C# 在Windows Phone 7中更改图像时内存泄漏,c#,silverlight,windows-phone-7,memory-leaks,C#,Silverlight,Windows Phone 7,Memory Leaks,在Windows Phone 7.5中多次更改图像容器的图像时出现问题 以下是错误代码: public void displayImages() { image1.Source = new System.Windows.Media.Imaging.BitmapImage (new Uri("BrainImg/axis/" + axis + currentSlice + ".jpg", UriKind.RelativeOrAbsolute));

在Windows Phone 7.5中多次更改图像容器的图像时出现问题

以下是错误代码:

public void displayImages() {
    image1.Source = new System.Windows.Media.Imaging.BitmapImage
       (new Uri("BrainImg/axis/" + axis + currentSlice + ".jpg",
             UriKind.RelativeOrAbsolute));
    image2.Source = new System.Windows.Media.Imaging.BitmapImage
       (new Uri("BrainImg/aseg/" + axis + currentSlice + ".png",
             UriKind.RelativeOrAbsolute));
}

private void slider1_ValueChanged(object sender, 
                                  RoutedPropertyChangedEventArgs<double> e)
{
    // do something
    if (this.slider1 != null)
    {
        currentSlice = (int) this.slider1.Value;
        displayImages();
    }
}
public void displayImages(){
image1.Source=new System.Windows.Media.Imaging.BitmapImage
(新Uri(“BrainImg/axis/”+axis+currentSlice+”.jpg),
相对溶质);
image2.Source=new System.Windows.Media.Imaging.BitmapImage
(新Uri(“BrainImg/aseg/”+axis+currentSlice+”.png),
相对溶质);
}
私有无效滑块1_值已更改(对象发送方,
RoutedPropertyChangedEventArgs(e)
{
//做点什么
如果(this.slider1!=null)
{
currentSlice=(int)this.slider1.Value;
显示图像();
}
}
在一些更改之后(大约100次,我的内存不足)


在分配新值之前,我已经尝试将
image.Source
设置为
null

很难从post中的代码片段中找出内存泄漏的原因。一个建议是寻找在生命周期较长的对象上订阅事件的短期对象。您应该分析您的应用程序,以查看托管内存中发生的情况,如对象存活等。查看blogpost,了解如何使用探查器检测内存问题。

映像控件的默认行为是缓存映像以备将来重用。这意味着控制器仍在使用内存。您需要显式释放对映像的引用以释放内存

像这样:

  BitmapImage bitmapImage = image.Source as BitmapImage;
  bitmapImage.UriSource = null;
  image.Source = null;

更多信息请参见:

这确实解决了问题。非常感谢你的回答。