Search Unity

Bug About Mesh object from MeshFilter.mesh is not destroyed.

Discussion in 'Editor & General Support' started by junimnjw, May 30, 2023.

  1. junimnjw

    junimnjw

    Joined:
    Apr 18, 2022
    Posts:
    3
    Hello.
    I am using MeshFilter.mesh property in my script.
    my problem is when I get the property value , I can see the number of mesh object increases continously when I run the same logic..
    I checked the gameboject that holds the MeshFilter component was successfully destoryed.. but the mesh object was still not destoryed.. is there any way to avoid the leak issue?
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,925
    MeshFilter.mesh
    returns an instance (copy) of the mesh when you access it. Accessing it multiple times is going to make multiple copies, and as these are
    UnityEngine.Objects
    , they are managed objects you need to destroy when no longer needed.

    Usually you access the property once to get that instance, and destroy the instance when no longer needed.

    Best to refer to the docs for this sort of thing: https://docs.unity3d.com/ScriptReference/MeshFilter-mesh.html
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,736
    Adding onto Spiney's excellent post...

    Usually the use cases are things like:

    - you have different meshes you want to assign, so you keep those Mesh references in your code and assign them to the MeshFilter when needed

    - you have different geometry (verts, tris, etc.), so you keep those arrays and feed them into the Mesh instance that you retrieved (or created!) and assigned to the MeshFilter

    What exactly is your use case for manipulating meshes?

    If you want more procgen examples, have a look around my MakeGeo project.

    MakeGeo is presently hosted at these locations:

    https://bitbucket.org/kurtdekker/makegeo

    https://github.com/kurtdekker/makegeo

    https://gitlab.com/kurtdekker/makegeo

    https://sourceforge.net/p/makegeo