Creation of PolygonPath/PolylinePath every frame during immediate drawing leaks Meshes (and memory).

Avatar
  • updated
  • Not a bug

Creating PolygonPath/PolylinePath every frame during immediate drawing such as  below leaks Mesh objects (and thus memory).

var polygon = new PolygonPath();
polygon.AddPoint(points[0]);
polygon.AddPoint(points[1]);
polygon.AddPoint(points[2]);
Draw.Polygon(polygon, Color.white);                               

(No references to polygon instance are retained in the user code).

Image 236

Reporting a bug? please specify Unity version:
2020.2.3f1
Reporting a bug? please specify Shapes version:
2.3.2
Reporting a bug? please specify Render Pipeline:
Pinned replies
Avatar
Freya Holmér creator
  • Answer
  • Not a bug

This is because polygon paths have to be disposed (you can find info on this in the documentation under immediate mode!)

this would be the correct syntax:

using( var polygon = new PolygonPath() ){
    polygon.AddPoint(points[0]);
    polygon.AddPoint(points[1]);
    polygon.AddPoint(points[2]);
    Draw.Polygon(polygon, Color.white);    
} // dispose is called here, destroying the mesh asset

Avatar
Freya Holmér creator

Just to add to this in case anyone finds it - there's currently an issue where even if you dispose of them in the editor (such as when drawing immediate mode shapes in the scene view), and you minimize Unity, it will actually never clear the meshes from memory, and it will keep allocating until you tab back. I'm not sure why Unity doesn't properly dispose of them.


A fix in the meantime is to turn off "always refresh" in the scene view

Image 681

Avatar
Koalefant

My bad, thank you!

Avatar
Freya Holmér creator
  • Answer
  • Not a bug

This is because polygon paths have to be disposed (you can find info on this in the documentation under immediate mode!)

this would be the correct syntax:

using( var polygon = new PolygonPath() ){
    polygon.AddPoint(points[0]);
    polygon.AddPoint(points[1]);
    polygon.AddPoint(points[2]);
    Draw.Polygon(polygon, Color.white);    
} // dispose is called here, destroying the mesh asset