Fixed memory leak. Formatting.

This commit is contained in:
NathanSweet 2013-08-16 12:22:55 +02:00
parent acf515cbb6
commit d641dbc414

View File

@ -37,24 +37,17 @@ namespace Spine {
static public class Util {
#if WINDOWS_STOREAPP
private static async Task<Texture2D> LoadFile(GraphicsDevice device, String path)
{
private static async Task<Texture2D> LoadFile(GraphicsDevice device, String path) {
var folder = Windows.ApplicationModel.Package.Current.InstalledLocation;
var file = await folder.GetFileAsync(path).AsTask().ConfigureAwait(false);
try
{
try {
return Util.LoadTexture(device, await file.OpenStreamForReadAsync().ConfigureAwait(false));
}
catch (Exception ex)
{
} catch (Exception ex) {
throw new Exception("Error reading texture file: " + path, ex);
}
}
static public Texture2D LoadTexture (GraphicsDevice device, String path)
{
static public Texture2D LoadTexture (GraphicsDevice device, String path) {
return LoadFile(device, path).Result;
}
#else
@ -67,8 +60,8 @@ namespace Spine {
}
}
}
#endif
static public Texture2D LoadTexture (GraphicsDevice device, Stream input) {
Texture2D file = Texture2D.FromStream(device, input);
@ -102,24 +95,22 @@ namespace Spine {
spriteBatch.Draw(file, file.Bounds, Color.White);
spriteBatch.End();
// Release the GPU back to drawing to the screen
// Release the GPU back to drawing to the screen.
device.SetRenderTarget(null);
spriteBatch.Dispose();
file.Dispose();
#if IOS
return result as Texture2D;
#else
// RenderTarget2D are volatile and will be lost on screen resolution changes.
// So instead of using this directly, we create a non-voliate Texture2D.
// This is computationally slower, but should be safe as long as it is done
// on load.
// This is computationally slower, but should be safe as long as it is done on load.
Texture2D resultTexture = new Texture2D(device, file.Width, file.Height);
Color[] resultContent = new Color[Convert.ToInt32(file.Width * file.Height)];
result.GetData(resultContent);
resultTexture.SetData(resultContent);
// Dispose of the RenderTarget2D immediately.
result.Dispose();
result.Dispose(); // Dispose of the RenderTarget2D immediately.
return resultTexture;
#endif
}