TechStark / opencv-js

OpenCV JavaScript version for node.js or browser
https://npmjs.com/@techstark/opencv-js
Apache License 2.0
408 stars 36 forks source link

Memory leakage when storing result back into source #56

Open jwlarocque opened 7 months ago

jwlarocque commented 7 months ago

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.