title: An unhandled exception of type 'System.AccessViolationException' occurred
body: <p>I have the following class:</p>
  
  <pre><code> public class RecipeItem
  {
      public Guid ID { get; set; }
      public string Title { get; set; }
      public string Instructions { get; set; }
      public string Ingredients { get; set; }
      public string ImagePath {get; set;}
  
      [XmlIgnore]
      public BitmapImage ListPreview { get; set; }
  
  }
  </code></pre>
  
  <p>That I serialize as such:</p>
  
  <pre><code>private void SaveRecipe()
      {
          fileName = recipeID + ".txt";
          recipe.Title = TitleBox.Text;
          recipe.Ingredients = Ingredients.Text;
          recipe.Instructions = Instructions.Text;
  
          string tempJPEG = "image" + recipeID + ".jpg";
  
          IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication();
          using (store = IsolatedStorageFile.GetUserStoreForApplication())
          {
              if (store.FileExists(tempJPEG))
              {
                  recipe.ImagePath = tempJPEG;
              }
  
  
              using (var file = store.CreateFile(recipe.ID + ".txt"))
              {
                  XmlSerializer serializer = new XmlSerializer(typeof(RecipeItem));
                  serializer.Serialize(file, recipe);
              }
          }
  
          store.Dispose();
      }
  </code></pre>
  
  <p>And finally deserialize into a List for a ListBox control as such:</p>
  
  <pre><code>       public static List&lt;RecipeItem&gt; CreateTestList()
      {
          List&lt;RecipeItem&gt; list = new List&lt;RecipeItem&gt;();
          RecipeItem recipe = new RecipeItem();
  
          //Get files from isolated store.
          IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication();
          try
          {
              XmlSerializer serializer = new XmlSerializer(typeof(RecipeItem));
              var filesName = store.GetFileNames();
              if (filesName.Length &gt; 0)
              {
                  foreach (string fileName in filesName)
                  {
                      if (fileName == "__ApplicationSettings") continue;
                      using (var file = store.OpenFile(fileName, FileMode.Open))
                      {
                          try
                          {
                              recipe = (RecipeItem)serializer.Deserialize(file);
                          }
                          catch
                          {
  
                          }
                      }
  
                      using (store = IsolatedStorageFile.GetUserStoreForApplication())
                      {
                          if (recipe.ImagePath!=null)
                          {
                              using (var stream = store.OpenFile(recipe.ImagePath, FileMode.Open, FileAccess.ReadWrite))
                              {
                                  recipe.ListPreview.SetSource(stream);
                                  recipe.ListPreview.DecodePixelHeight = 100;
                                  recipe.ListPreview.DecodePixelWidth = 100;                                
                              }
  
                          }
                      }
                  }
              }
          }
          catch
          {
  
          }
  
          list.Add(recipe);
          store.Dispose();
          return list;
      }
  </code></pre>
  
  <p>I keep getting a System.AccessViolationException on the line of code:</p>
  
  <pre><code> recipe.ListPreview.SetSource(stream);
  </code></pre>
  
  <p>Basically what I am trying to do here is allow a user defined Image to bind to a ListBox. Because you can't serialize a BitmapImage, I instead save the file into IsolatedStorage and save the path into a string called ImagePath. When I deserialize to create a List for my ListBox, I take the image path and open the image file and set it to the source of a BitmapImage that then binds to a ListBox. Everything in my code works fine except that one line of code, serialization and deserialzation both work flawlessly and binding the Image file to an Image control directly from IsolatedStorage works perfectly as well.</p>
  
  <p>What do you think may be causing the AccessViolationException? </p>
  
  <p>Thanks in advance!</p>