Execute in edit mode causing memory leaks?

I’ve noticed that when I use ExecuteInEditMode and I’m creating a mesh procedurally that I get the “Cleaning up leaked objects in scene…” message every time I save.

It says Mesh is being leaked and no matter when I do I can’t get it to not leak. I’ve tried destroying the Mesh, setting it to null, everything I can think of but that message always comes up. I don’t see this happening in other in-editor tools like RageSpline so it’s something I’m doing wrong… anyone have any ideas?

Here’s the code, it’s just a simple ring mesh maker:

using UnityEngine;
using System.Collections.Generic;

[ExecuteInEditMode]
public class RingMeshScript : MonoBehaviour {
	public bool elliptical;
	
	private Mesh ringMesh;
	private List<Vector3> ringVerts = new List<Vector3>();
	private List<Vector2> ringUVs = new List<Vector2>();
	private List<int> ringTris = new List<int>();
	
	public int sections = 20;
	private int oldSections;
	public float xRadius = 10;
	private float oldXRadius;
	public float yRadius = 10;
	private float oldYRadius;
	public float thickness = 1;
	private float oldThickness;

	void Start () {
		DestroyImmediate(ringMesh);
		
		oldSections = sections;
		oldXRadius = xRadius;
		oldYRadius = yRadius;
		oldThickness = thickness;
		CreateMesh();
	}
	
	void Update () {
		if(!elliptical){
			oldYRadius = yRadius = xRadius;
		}
		
		if(sections != oldSections){
			CreateMesh ();
		}
		if(xRadius != oldXRadius || yRadius != oldYRadius || thickness != oldThickness){
			UpdateMesh();
		}
		
		oldSections = sections;
		oldXRadius = xRadius;
		oldYRadius = yRadius;
		oldThickness = thickness;
	}
		
	void CreateMesh () {
		DestroyImmediate(ringMesh);
		ringVerts.Clear();
		ringUVs.Clear ();
		ringTris.Clear ();
		ringMesh = new Mesh();
		float incriments = (float)360/sections;
		float uvIncriments = (float)1/sections;
		
		for(int i = 0; i < sections + 1; i++){
			float x = transform.position.x + (xRadius - thickness) * Mathf.Cos ( incriments * i * Mathf.PI / 180);
			float y = transform.position.y + (yRadius - thickness) * Mathf.Sin (incriments * i * Mathf.PI / 180);
			Vector3 newVert = new Vector3(x, y, transform.position.z);
			ringVerts.Add (newVert);
			Vector2 newUV = new Vector2(uvIncriments * i, 1);
			ringUVs.Add(newUV);
			
			x = transform.position.x + (xRadius + thickness) * Mathf.Cos (incriments * i * Mathf.PI / 180);
			y = transform.position.y + (yRadius + thickness) * Mathf.Sin (incriments * i * Mathf.PI / 180);
			newVert = new Vector3(x, y, transform.position.z);
			ringVerts.Add (newVert);
			newUV = new Vector2(uvIncriments * i, 0);
			ringUVs.Add(newUV);
		}
		ringMesh.vertices = ringVerts.ToArray();
		ringMesh.uv = ringUVs.ToArray();
		for(int i = 0; i < sections * 2; i++){
			if(i%2 == 0){
				ringTris.Add (i + 2);
				ringTris.Add (i);
				ringTris.Add (i + 1);
				
			}
			if(i%2 == 1){
				ringTris.Add (i);
				ringTris.Add (i + 2);
				ringTris.Add (i + 1);
			}
		}
		ringMesh.triangles = ringTris.ToArray();
		ringMesh.RecalculateNormals();
		ringMesh.RecalculateBounds();
		GetComponent<MeshFilter>().mesh = ringMesh;
	}
	
	void UpdateMesh () {
		ringVerts.Clear();
		float incriments = (float)360/sections;
		for(int i = 0; i < sections + 1; i++){
			float x = transform.position.x + (xRadius - thickness) * Mathf.Cos (incriments * i * Mathf.PI / 180);
			float y = transform.position.y + (yRadius - thickness) * Mathf.Sin (incriments * i * Mathf.PI / 180);
			Vector3 newVert = new Vector3(x, y, transform.position.z);
			ringVerts.Add (newVert);
			
			x = transform.position.x + (xRadius + thickness) * Mathf.Cos (incriments * i * Mathf.PI / 180);
			y = transform.position.y + (yRadius + thickness) * Mathf.Sin (incriments * i * Mathf.PI / 180);
			newVert = new Vector3(x, y, transform.position.z);
			ringVerts.Add (newVert);
		}
		ringMesh.vertices = ringVerts.ToArray();
		ringMesh.RecalculateBounds();
	}
}

Try this:

Remove at line 51:

DestroyImmediate(ringMesh);

And add at line 55:

ringMesh = new Mesh(); ringMesh.hideFlags = HideFlags.HideAndDontSave;

You should set MeshFilter.sharedMesh, not MeshFilter.mesh.

Thanks guys. For anyone who runs into a similar issue, superpig was correct. Since I’m creating a mesh, that mesh was being added to my hierarchy and when I deleted just “mesh” it was just deleting the instance. Deleting sharedMesh deletes the whole thing.