2013-08-21 9 views
6

私はWriteableBitmapの任意のインスタンスを作成するたびに、私はメモリリークを抱えています。私はstackoverflowや他のフォーラムで複数の提案を試みましたが、何も動作していません。WriteableBitmapメモリリーク8

  1. PhotoChooserTask
  2. WriteableBitmapを作成するPhotoResultオブジェクトからStreamを使用して画像を選択します。私のテストのアプリの基本的な流れはこれです。

これだけです。変数をヌルにしてGC.Collect()を呼び出すと、問題の一部しか解決しません。アプリがクラッシュするまでアプリがメモリを割り当てないようにしますが、オブジェクトがスコープから外れても、新しい画像を選択するまで、メモリは常に割り当てられます。私は、XAMLアプリケーションを備えたデフォルトのWindows Phone Direct3Dでそれを再現することができます。デフォルトのプロジェクトへの唯一の変更は以下のとおりである。このため

MainPage.xaml.cs

public MainPage() { 
    InitializeComponent(); 
    _photoChooserTask = new PhotoChooserTask(); 
    _photoChooserTask.Completed += new EventHandler<PhotoResult>(photoChooserTaskComplete); 
} 

private void ApplicationBarIconButton_Click(object sender, EventArgs e) { 
    _photoChooserTask.Show(); 
} 

private void photoChooserTaskComplete(object sender, PhotoResult e) { 
    if (e.TaskResult == TaskResult.OK) { 
     BitmapImage image = new BitmapImage(); 
     image.SetSource(e.ChosenPhoto); 
     WriteableBitmap wbm = new WriteableBitmap(image); 
     image.UriSource = null; 
     image = null; 
     wbm = null; 
     GC.Collect(); 
    } 
} 

MainPage.xamlを

<phone:PhoneApplicationPage.ApplicationBar> 
    <shell:ApplicationBar IsVisible="True" IsMenuEnabled="True" Mode="Default" Opacity="0.5" > 
     <shell:ApplicationBar.Buttons> 
      <shell:ApplicationBarIconButton IconUri="/junkUrl.png" Text="albums" Click="ApplicationBarIconButton_Click" /> 
     </shell:ApplicationBar.Buttons> 
    </shell:ApplicationBar> 
</phone:PhoneApplicationPage.ApplicationBar> 
+0

こんにちは、私はこの問題にも遭遇しました。 –

答えて

-1

あなたはIsolatedStorege内でこのファイルストリームを保存する必要があります。したがって、このように、IsolatedStorageFileStreamを使用してFILESTREAMを作成し、それを保存...

private void photoChooserTaskComplete(object sender, PhotoResult e) { 
if (e.TaskResult == TaskResult.OK) { 
     SaveToIsolatedStorage(e.ChosenPhoto,"Your File Name");   
} 

}

public void SaveToIsolatedStorage(Stream imageStream, string fileName) 
    { 
     try 
     { 
      string imagename = fileName + ".jpg"; 
      using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication()) 
      { 
       if (myIsolatedStorage.FileExists(imagename)) 
       { 
        myIsolatedStorage.DeleteFile(imagename); 
       } 
       IsolatedStorageFileStream fileStream = myIsolatedStorage.CreateFile(imagename); 
       WriteableBitmap wb = new WriteableBitmap(100, 100); 
       wb.SetSource(imageStream); 
       wb.SaveJpeg(fileStream, 100, 100, 0, 70); 
       fileStream.Close(); 
      } 
     } 

     catch (Exception) 
     { 
      RadMessageBox.Show(String.Empty, MessageBoxButtons.OK, "Error occured while saving Images");        
     } 

    } 

、読書のためにあなたはIsolatedStorage

public WriteableBitmap ReadFromIsolatedStorage(string fileName) 
    { 
     WriteableBitmap bitmap = new WriteableBitmap(100, 100); 
     try 
     { 
      using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication()) 
      { 
       if (myIsolatedStorage.FileExists(fileName)) 
       { 

        using (IsolatedStorageFileStream fileStream = myIsolatedStorage.OpenFile(fileName, FileMode.Open, FileAccess.Read)) 
        { 
         // Decode the JPEG stream.        
         bitmap = PictureDecoder.DecodeJpeg(fileStream, 100, 100); 
        } 
       } 
      } 
     } 
     catch (Exception) 
     { 
      RadMessageBox.Show(String.Empty, MessageBoxButtons.OK, "Error Occcured while reading image");        
     } 


     return bitmap; 
    } 

からこの意志をそのファイルを取得することができますあなたのメモリリークの問題を解決し、これを試してみてください...

+1

上記は動作しません。指示通りに私はそれを実装しましたが、オブジェクトがスコープから外れるとメモリは解放されません。最初のストリームから作成されたWriteableBitmapは、別のイメージを選択して新しいオブジェクトを作成するまでは収集されません。隔離されたストレージからイメージを読み取ることは、正常であり、予想されるガベージコレクションの動作に従っているようです。 – Wagan8r