Bug 379374 gets no love. Mesh memory leak when modifying uvs

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 :wink:

Even though you haven’t fixed them, I’ve still got nothing but love for Unity! :smile:

Although the behavior is a little counterintuitive, I’m not sure that’s a bug.

Things like meshes and materials have an engine-level representation in addition to a ‘Mono’ representation, and just because the Mono representation is garbage-collected doesn’t necessarily mean the engine-level representation will be cleaned up.

For example, the .material and .mesh properties, when accessed, make a copy of the referenced object and return a reference to that copy so that you can modify it if you wish without affecting the original shared resource. AFAIK, all such copies are cleaned up when a scene is unloaded, but not before. As such, if you’re creating a lot of temporary resources of this type, memory usage can pile up as a scene is running.

I’m not completely sure this is the cause of the problems you’re seeing, but I thought I’d mention it just in case.

(Also, I think the iPhone version of Unity includes some ‘manual cleanup’ functions that the desktop version doesn’t, but I could be wrong about that.)

You’re probably thinking of Application.GarbageCollectUnusedAssets, which never existed in desktop Unity; however that’s been replaced with Resources.UnloadUnusedAssets in Unity 3, and there are no separate versions anymore.

–Eric

then it’s a bug in the documentation. :smile:

I’d say if anything, it’s more of an omission than a bug. (Unless there’s something in the documentation that actually incorrectly describes the behavior in question.)

Does anybody have a workaround for this? I’m running into the same problem, and our game is crashing after awhile.

Funny that you necro-ed this post 5 minutes after I submitted a bug report about something similar.

I’m having this issue when working with Trail Renderers. (Possibly the auto-destruct is being screwy?)