I have a memory crash when calling Resources.UnloadUnusedAssets on Android (using Unity 5.1.1 but in previous versions as well). The app just closes without any error message.
The game shows photo's taken by other players so the player can like or dislike them.
So this is the general flow, which is repeated:
- load a texture:
byte[] fileData = File.ReadAllBytes(filePath);
Texture2D tex = new Texture2D(2, 2);
tex.LoadImage(fileData);
- show the texture on screen (using tk2dSpriteFromTexture of the 2D Toolkit plugin)
- destroy the texture:
UnityEngine.Texture2D.Destroy (tex);
- call Resources.UnloadUnusedAssets and GC.Collect():
protected static IEnumerator DoMemoryCleanupCoroutine(Action callback)
{
yield return Resources.UnloadUnusedAssets();
yield return null;
GC.Collect();
yield return null;
if(callback != null)
{
callback();
}
}
After executing this 100-120 times, the app crashes when calling Resources.UnloadUnusedAssets. The crash will not happen randomly: if I do the same flow for the same amount of times on multiple devices they will all crash at the same moment.
I have attached the profiler and added debug logging to track the memory usage, and there seems no increase or anything strange with that. So it doesn't seem to be a memory leak.
I've also tried to create a texture pool so the Texture2D object is kept in memory and not recreated every time, but this does not seem to have any influence.
It seems that there is a bug in Resources.UnloadUnusedAssets that makes it crash after calling a certain amount of times on Android...
On iOS this does not happen.
Any idea to solve this?
↧