Backgroundsubtractor: protect async against release of input mat

This commit is contained in:
Simon Hailes 2017-11-12 15:45:03 +00:00
parent f7d6b6cdee
commit e0087841bd

View File

@ -313,13 +313,15 @@ public:
AsyncBackgroundSubtractorWorker( AsyncBackgroundSubtractorWorker(
Nan::Callback *callback, Nan::Callback *callback,
BackgroundSubtractorWrap *bg, BackgroundSubtractorWrap *bg,
Matrix *img) : cv::Mat &img_mat):
Nan::AsyncWorker(callback), Nan::AsyncWorker(callback),
bg(bg), bg(bg),
img(img) { img_mat(img_mat) { // note: this makes a new cv::Mat, and so increments the ref count for the data without copying it
} }
~AsyncBackgroundSubtractorWorker() { ~AsyncBackgroundSubtractorWorker() {
// upon destroy, img_mat will reduce refcount on data by one
} }
// Executed inside the worker-thread. // Executed inside the worker-thread.
@ -330,9 +332,9 @@ public:
// wait here if already in apply - auto-release on scope exit // wait here if already in apply - auto-release on scope exit
BGAutoMutex(bg->applymutex); BGAutoMutex(bg->applymutex);
#if CV_MAJOR_VERSION >= 3 #if CV_MAJOR_VERSION >= 3
bg->subtractor->apply(this->img->mat, _fgMask); bg->subtractor->apply(this->img_mat, _fgMask);
#else #else
bg->subtractor->operator()(this->img->mat, _fgMask); bg->subtractor->operator()(this->img_mat, _fgMask);
#endif #endif
} }
@ -360,7 +362,7 @@ public:
private: private:
BackgroundSubtractorWrap *bg; BackgroundSubtractorWrap *bg;
Matrix *img; cv::Mat img_mat;
cv::Mat _fgMask; cv::Mat _fgMask;
}; };
@ -400,7 +402,7 @@ NAN_METHOD(BackgroundSubtractorWrap::Apply) {
Nan::Callback *callback = new Nan::Callback(cb.As<Function>()); Nan::Callback *callback = new Nan::Callback(cb.As<Function>());
Matrix *_img = Nan::ObjectWrap::Unwrap<Matrix>(info[0]->ToObject()); Matrix *_img = Nan::ObjectWrap::Unwrap<Matrix>(info[0]->ToObject());
Nan::AsyncQueueWorker(new AsyncBackgroundSubtractorWorker( callback, self, _img)); Nan::AsyncQueueWorker(new AsyncBackgroundSubtractorWorker( callback, self, _img->mat));
return; return;
} else { //synchronous - return the image } else { //synchronous - return the image