Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WP8] fix memory leak when Resume game #3725

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions MonoGame.Framework/Content/ContentManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public partial class ContentManager : IDisposable
private IServiceProvider serviceProvider;
private IGraphicsDeviceService graphicsDeviceService;
private Dictionary<string, object> loadedAssets = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);
internal Dictionary<string, object> loadedSharedResources = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);
private List<IDisposable> disposableAssets = new List<IDisposable>();
private bool disposed;
private byte[] scratchBuffer;
Expand Down Expand Up @@ -446,6 +447,7 @@ public virtual void Unload()
}
disposableAssets.Clear();
loadedAssets.Clear();
loadedSharedResources.Clear();
}

public string RootDirectory
Expand Down
11 changes: 10 additions & 1 deletion MonoGame.Framework/Content/ContentReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,16 @@ internal void ReadSharedResources()

var sharedResources = new object[sharedResourceCount];
for (var i = 0; i < sharedResourceCount; ++i)
sharedResources[i] = InnerReadObject<object>(null);
{
object existingInstance;
string key = assetName.Replace('\\', '/') +"_SharedResource_" + i;
contentManager.loadedSharedResources.TryGetValue(key, out existingInstance);

sharedResources[i] = InnerReadObject<object>(existingInstance);

if (existingInstance == null)
contentManager.loadedSharedResources[key] = sharedResources[i];
}

// Fixup shared resources by calling each registered action
foreach (var fixup in sharedResourceFixups)
Expand Down
8 changes: 6 additions & 2 deletions MonoGame.Framework/Content/ContentReaders/EffectReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,14 @@ public EffectReader()

protected internal override Effect Read(ContentReader input, Effect existingInstance)
{
int dataSize = input.ReadInt32();
var effect = existingInstance;

int dataSize = (int)input.ReadUInt32();
byte[] data = input.ContentManager.GetScratchBuffer(dataSize);
input.Read(data, 0, dataSize);
var effect = new Effect(input.GraphicsDevice, data, 0, dataSize);

if (effect == null)
effect = new Effect(input.GraphicsDevice, data, 0, dataSize);
effect.Name = input.AssetName;
return effect;
}
Expand Down
8 changes: 6 additions & 2 deletions MonoGame.Framework/Content/ContentReaders/SongReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ internal class SongReader : ContentTypeReader<Song>
{
protected internal override Song Read(ContentReader input, Song existingInstance)
{
var path = input.ReadString();
var song = existingInstance;

var path = input.ReadString();

if (!String.IsNullOrEmpty(path))
{
Expand All @@ -27,7 +29,9 @@ protected internal override Song Read(ContentReader input, Song existingInstance

var durationMs = input.ReadObject<int>();

return new Song(path, durationMs);
if (song == null)
song = new Song(path, durationMs);
return song;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ protected internal override SoundEffect Read(ContentReader input, SoundEffect ex
// Create the effect.
var effect = new SoundEffect(header, data, dataSize, durationMs, loopStart, loopLength);

if(existingInstance != null)
return existingInstance;
// Store the original asset name for debugging later.
effect.Name = input.AssetName;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@ class VertexBufferReader : ContentTypeReader<VertexBuffer>
{
protected internal override VertexBuffer Read(ContentReader input, VertexBuffer existingInstance)
{
var buffer = existingInstance;

var declaration = input.ReadRawObject<VertexDeclaration>();
var vertexCount = (int)input.ReadUInt32();
int dataSize = vertexCount * declaration.VertexStride;
byte[] data = input.ContentManager.GetScratchBuffer(dataSize);
input.Read(data, 0, dataSize);

var buffer = new VertexBuffer(input.GraphicsDevice, declaration, vertexCount, BufferUsage.None);
if(buffer == null)
buffer = new VertexBuffer(input.GraphicsDevice, declaration, vertexCount, BufferUsage.None);
buffer.SetData(data, 0, dataSize);
return buffer;
}
Expand Down