opencv-js icon indicating copy to clipboard operation
opencv-js copied to clipboard

Memory leakage when storing result back into source

Open jwlarocque opened this issue 1 year ago • 0 comments

I noticed a memory leak in my application when applying an opencv function to a Mat and storing the result back in the same Mat. Specifically this is within a web worker, not sure whether that makes a difference.
For example:

let mat = cv.matFromArray(rows, cols, cv.CV_32FC1, dataArray);
cv.resize(mat, mat, new_size, cv.INTER_LINEAR); // <- this leaks (it's not specific to `resize`, just an example)
mat.delete();

Fixed:

let mat_a = cv.matFromArray(rows, cols, cv.CV_32FC1, dataArray);
let mat_b = new cv.Mat();
cv.resize(mat_a, mat_b, new_size, cv.INTER_LINEAR);
// you can keep alternating `mat_a` and `mat_b` if you have multiple transforms, and it won't leak
mat_a.delete();
mat_b.delete();

I don't have a minimum repro or anything at the moment but I wanted to record this somewhere in case it's helpful to someone else.

jwlarocque avatar Mar 02 '24 08:03 jwlarocque