Matrix: resizeasync - make safe to mat.release() before async is complete

This commit is contained in:
Simon Hailes 2017-11-12 15:58:17 +00:00
parent 31c9349885
commit 642bf94a3d

View File

@ -1893,9 +1893,9 @@ cv::Rect* setRect(Local<Object> objRect, cv::Rect &result) {
class ResizeASyncWorker: public Nan::AsyncWorker { class ResizeASyncWorker: public Nan::AsyncWorker {
public: public:
ResizeASyncWorker(Nan::Callback *callback, Matrix *image, cv::Size size, double fx, double fy, int interpolation) : ResizeASyncWorker(Nan::Callback *callback, cv::Mat image, cv::Size size, double fx, double fy, int interpolation) :
Nan::AsyncWorker(callback), Nan::AsyncWorker(callback),
image(image), image(image), // here, the cv::Mat is duplicated, adding to refcount without data copy
dest(NULL), dest(NULL),
size(size), size(size),
fx(fx), fx(fx),
@ -1909,12 +1909,13 @@ public:
// could happen if NaN does not call HandleSuccess? // could happen if NaN does not call HandleSuccess?
delete dest; delete dest;
dest = NULL; dest = NULL;
// cv::Mat image will be deleted, which will reduce refcount
} }
void Execute() { void Execute() {
try { try {
dest = new Matrix(); dest = new Matrix();
cv::resize(image->mat, dest->mat, size, fx, fy, interpolation); cv::resize(image, dest->mat, size, fx, fy, interpolation);
success = 1; success = 1;
} catch(...){ } catch(...){
success = 0; success = 0;
@ -1974,7 +1975,7 @@ public:
} }
private: private:
Matrix *image; cv::Mat image;
Matrix *dest; Matrix *dest;
cv::Size size; cv::Size size;
double fx; double fx;
@ -2040,7 +2041,7 @@ NAN_METHOD(Matrix::Resize) {
if (isAsync){ if (isAsync){
REQ_FUN_ARG(numargs-1, cb); REQ_FUN_ARG(numargs-1, cb);
Nan::Callback *callback = new Nan::Callback(cb.As<Function>()); Nan::Callback *callback = new Nan::Callback(cb.As<Function>());
Nan::AsyncQueueWorker(new ResizeASyncWorker(callback, self, size, fx, fy, interpolation)); Nan::AsyncQueueWorker(new ResizeASyncWorker(callback, self->mat, size, fx, fy, interpolation));
info.GetReturnValue().Set(Nan::Null()); info.GetReturnValue().Set(Nan::Null());
} else { } else {
try{ try{