This bug has been open for 3 months so I thought I’d post it here just in case someone has encountered the same issue. It would be tough to track down the bug/fix without the pro version of Unity. This was causing my iphone game to crash so no, GC doesn’t eventually kick in.
If you modify a mesh’s uvs the mesh won’t get garbage collected when you destroy the gameobject. I’ve included some code so you can see for yourself. Just attach the script to the main camera and give it a prefab with a mesh. If you comment out the UV part the mesh leaks will get garbage collected.
using UnityEngine;
using System.Collections;
public class NewBehaviourScript : MonoBehaviour {
public GameObject mPrefab;
private GameObject mGameObject;
// Use this for initialization
void Start ()
{
InvokeRepeating("Build", 0, 0.5f);
InvokeRepeating("Destroy", 0.25f, 0.5f);
}
void Build()
{
if (this.mGameObject != null)
{
Destroy();
return;
}
this.mGameObject = GameObject.Instantiate(this.mPrefab) as GameObject;
//Comment out and no memory leak
MeshFilter mf = this.mGameObject.GetComponent(typeof(MeshFilter)) as MeshFilter;
Vector2[] uvs = mf.mesh.uv;
for (int i = 0; i < uvs.Length; i++)
{
uvs[i] = new Vector2(Random.Range(-1, 1), Random.Range(-1, 1));
}
mf.mesh.uv = uvs;
//end
}
void Destroy()
{
if (this.mGameObject == null) return;
GameObject.Destroy(this.mGameObject);
}
}
you can also destroy the mesh before you destroy the gameobject, but shouldn’t this be what unity does automatically?
MeshFilter mf = this.mGameObject.GetComponent(typeof(MeshFilter)) as MeshFilter;
GameObject.Destroy(mf.mesh);
GameObject.Destroy(this.mGameObject);
379379 is also a memeory leak bug and could use some love too ![]()
Even though you haven’t fixed them, I’ve still got nothing but love for Unity! ![]()