I’m trying to generate a mesh with the jobs system and I keep seeing memory leak detected warnings.
I have a hashmap of nodes which will build the mesh data.
nodes = new NativeParallelHashMap<int4, Node>(size * size * height * scale * scale * scale, Allocator.Persistent);
And my job to bulid the mesh looks like this.
[BurstCompile]
public struct Refresh : IJobParallelFor
{
[ReadOnly] public NativeParallelHashMap<int4, Node> nodes;
[ReadOnly] public int scale;
NativeStream.Writer verts;
NativeStream.Writer tris;
NativeStream.Writer uvs;
public void Execute(int y)
{
verts.BeginForEachIndex(y);
tris.BeginForEachIndex(y);
uvs.BeginForEachIndex(y);
for(int x = 0; x < scale; x++) {
for (int z = 0; z < scale; z++) {
nodes.TryGetValue(new(x,y,z,0), out Node node);
Node.Build(ref node, ref verts, ref tris, ref uvs);
}
}
verts.EndForEachIndex();
tris.EndForEachIndex();
uvs.EndForEachIndex();
}
public static void Schedule(_Chunk chunk)
{
// declare our data
var job = new Refresh(){ };
job.scale = _.map.scale;
job.nodes = _.map.nodes;
MeshBuilder mb = new MeshBuilder(_.map.scale);
job.verts = mb.verts.AsWriter();
job.tris = mb.tris.AsWriter();
job.uvs = mb.uvs.AsWriter();
// schedule job
var handle = job.Schedule(_.map.scale, 1);
handle.Complete();
// cleanup
mb.verts.Dispose();
mb.tris.Dispose();
mb.uvs.Dispose();
}
}
public struct MeshBuilder
{
public NativeStream verts;
public NativeStream tris;
public NativeStream uvs;
public MeshBuilder(int buffers)
{
verts = new NativeStream(buffers, Allocator.TempJob);
tris = new NativeStream(buffers, Allocator.TempJob);
uvs = new NativeStream(buffers, Allocator.TempJob);
}
}
If I allocate the node hashmap as persistent and I dispose of the nativestreams after the job then there shouldn’t be any leaks right?