Error when drawing from threads with Aline

I’m trying a draw a number of lines from parallel threads. As per the documentation I am creating a custom CommandBuilder on the main thread and disposing of it after drawing. Unfortunately, I am getting a bunch of “Exception: Too few pop commands and too many push commands.” errors.

My code looks like this:

 private void ApproximateContours(int height) {
    CommandBuilder builder = DrawingManager.GetBuilder(true);
    
    using (builder.InScreenSpace(FrameMatProcessor.ARCamera))
    using (builder.WithColor(Color.green)) {
        Parallel.For(0, contours.Count, (int index) => {
            using MatOfPoint2f contour = new MatOfPoint2f();
            contours[index].convertTo(contour, CvType.CV_32FC2);

            using MatOfPoint2f simpleContour = new MatOfPoint2f();
            Imgproc.approxPolyDP(contour, simpleContour, 1f, true);

            float3 first = new float3(simpleContour.at<float2>(0, 0), 0);
            first.y = height - first.y;

            float3 prev = first;
            for (int i = 1; i < simpleContour.rows(); i++) {
                float3 cur = new float3(simpleContour.at<float2>(i, 0), 0);
                cur.y = height - cur.y;

                builder.Line(prev, cur);
                prev = cur;
            }

           builder.Line(prev, first);
        });
    }
    builder.Dispose();
}

Hi

You’ll need one builder per thread.
If you use the unity job system, you only need a single builder, though.

Ah, yes I thought I might need to do that. Would allocating an array of builders on the main thread be the way to go?

Yes, that would work.

Alright, I will do that then, Cheers