Description: avoid occasional crash in async call to opencv
Author: Jochen Sprickerhof <jspricke@debian.org>
Reviewed-By: Jérémy Lal <kapouer@melix.org>
Last-Update: 2021-04-30
Forwarded: https://github.com/peterbraden/node-opencv/pull/679
--- a/src/OpenCV.cc
+++ b/src/OpenCV.cc
@@ -37,6 +37,7 @@
         cv::Mat mbuf(len, 1, CV_64FC1, buf);
         outputmat = cv::imdecode(mbuf, flags);
         success = 1;
+        free(buf);
       } catch(...){
         success = 0;
       }
@@ -224,8 +225,10 @@
         // async
         uint8_t *buf = (uint8_t *) Buffer::Data(Nan::To<v8::Object>(info[0]).ToLocalChecked());
         unsigned len = Buffer::Length(Nan::To<v8::Object>(info[0]).ToLocalChecked());
+        uint8_t *buf_new = (uint8_t *)malloc(len);
+        memcpy(buf_new, buf, len);
         Nan::Callback *callback = new Nan::Callback(cb.As<Function>());
-        Nan::AsyncQueueWorker(new AsyncImDecodeWorker(callback, buf, len, flags));
+        Nan::AsyncQueueWorker(new AsyncImDecodeWorker(callback, buf_new, len, flags));
         return;
       }
       // WILL have returned by here unless exception
