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).

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
  • 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
  • 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
Koalefant

My bad, thank you!