From ab0bd505d2a667dcb22e3b64fb936bda434bfb7e Mon Sep 17 00:00:00 2001 From: Simon Hailes Date: Fri, 10 Nov 2017 09:49:12 +0000 Subject: [PATCH 1/4] resize(async): seems we can get an assert in Unwrap if image has 0,0, size, so catch and report as a callback error. --- src/Matrix.cc | 57 +++++++++++++++++++++++++++++++++------------------ 1 file changed, 37 insertions(+), 20 deletions(-) diff --git a/src/Matrix.cc b/src/Matrix.cc index 962230a..9a5839d 100755 --- a/src/Matrix.cc +++ b/src/Matrix.cc @@ -1919,22 +1919,35 @@ public: Nan::HandleScope scope; if (success){ - Local im_to_return= Nan::NewInstance(Nan::GetFunction(Nan::New(Matrix::constructor)).ToLocalChecked()).ToLocalChecked(); - Matrix *img = Nan::ObjectWrap::Unwrap(im_to_return); - img->mat = dest->mat; - delete dest; - - //delete dest; - - Local argv[] = { - Nan::Null(), // err - im_to_return //result - }; + try{ + Local im_to_return= Nan::NewInstance(Nan::GetFunction(Nan::New(Matrix::constructor)).ToLocalChecked()).ToLocalChecked(); + Matrix *img = Nan::ObjectWrap::Unwrap(im_to_return); + img->mat = dest->mat; + delete dest; - Nan::TryCatch try_catch; - callback->Call(2, argv); - if (try_catch.HasCaught()) { - Nan::FatalException(try_catch); + //delete dest; + + Local argv[] = { + Nan::Null(), // err + im_to_return //result + }; + + Nan::TryCatch try_catch; + callback->Call(2, argv); + if (try_catch.HasCaught()) { + Nan::FatalException(try_catch); + } + } catch (...){ + Local argv[] = { + Nan::New("C++ exception wrapping response").ToLocalChecked(), // err + Nan::Null() // result + }; + + Nan::TryCatch try_catch; + callback->Call(2, argv); + if (try_catch.HasCaught()) { + Nan::FatalException(try_catch); + } } } else { delete dest; @@ -2022,11 +2035,15 @@ NAN_METHOD(Matrix::Resize) { Nan::AsyncQueueWorker(new ResizeASyncWorker(callback, self, size, fx, fy, interpolation)); info.GetReturnValue().Set(Nan::Null()); } else { - Matrix *self = Nan::ObjectWrap::Unwrap(info.This()); - cv::Mat res = cv::Mat(x, y, CV_32FC3); - cv::resize(self->mat, res, cv::Size(x, y), 0, 0, interpolation); - ~self->mat; - self->mat = res; + try{ + Matrix *self = Nan::ObjectWrap::Unwrap(info.This()); + cv::Mat res = cv::Mat(x, y, CV_32FC3); + cv::resize(self->mat, res, cv::Size(x, y), 0, 0, interpolation); + ~self->mat; + self->mat = res; + } catch (...){ + return Nan::ThrowError("c++ Exception processing resize"); + } } } From f2c05255a03c4ec0bfd59bbcfc7c654ac8a913d4 Mon Sep 17 00:00:00 2001 From: Simon Hailes Date: Fri, 10 Nov 2017 10:25:27 +0000 Subject: [PATCH 2/4] resize: delete the dest we created on error, and if still present on destructor --- src/Matrix.cc | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/Matrix.cc b/src/Matrix.cc index 9a5839d..f397690 100755 --- a/src/Matrix.cc +++ b/src/Matrix.cc @@ -1899,10 +1899,14 @@ public: fx(fx), fy(fy), interpolation(interpolation), - success(0){ + success(0), + dest(null){ } ~ResizeASyncWorker() { + // don't leave this if it was allocated + // could happen if NaN does not call HandleSuccess? + delete dest; } void Execute() { @@ -1924,8 +1928,7 @@ public: Matrix *img = Nan::ObjectWrap::Unwrap(im_to_return); img->mat = dest->mat; delete dest; - - //delete dest; + dest = null; Local argv[] = { Nan::Null(), // err @@ -1938,6 +1941,8 @@ public: Nan::FatalException(try_catch); } } catch (...){ + delete dest; + dest = null; Local argv[] = { Nan::New("C++ exception wrapping response").ToLocalChecked(), // err Nan::Null() // result @@ -1951,6 +1956,7 @@ public: } } else { delete dest; + dest = null; Local argv[] = { Nan::New("C++ exception").ToLocalChecked(), // err From 0b6b1f42cc4d86465ea7e7cc0c1a05e2813b36eb Mon Sep 17 00:00:00 2001 From: Simon Hailes Date: Fri, 10 Nov 2017 10:28:56 +0000 Subject: [PATCH 3/4] fix stupid typo --- src/Matrix.cc | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/Matrix.cc b/src/Matrix.cc index f397690..ab6f3dc 100755 --- a/src/Matrix.cc +++ b/src/Matrix.cc @@ -1900,13 +1900,14 @@ public: fy(fy), interpolation(interpolation), success(0), - dest(null){ + dest(NULL){ } ~ResizeASyncWorker() { // don't leave this if it was allocated // could happen if NaN does not call HandleSuccess? delete dest; + dest = NULL; } void Execute() { @@ -1928,7 +1929,7 @@ public: Matrix *img = Nan::ObjectWrap::Unwrap(im_to_return); img->mat = dest->mat; delete dest; - dest = null; + dest = NULL; Local argv[] = { Nan::Null(), // err @@ -1942,7 +1943,7 @@ public: } } catch (...){ delete dest; - dest = null; + dest = NULL; Local argv[] = { Nan::New("C++ exception wrapping response").ToLocalChecked(), // err Nan::Null() // result @@ -1956,7 +1957,7 @@ public: } } else { delete dest; - dest = null; + dest = NULL; Local argv[] = { Nan::New("C++ exception").ToLocalChecked(), // err From 6393a7f44e93ee5f07aa0501fe1bdaf72984a0b7 Mon Sep 17 00:00:00 2001 From: Simon Hailes Date: Fri, 10 Nov 2017 10:32:51 +0000 Subject: [PATCH 4/4] avoid warning about initilisation order --- src/Matrix.cc | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Matrix.cc b/src/Matrix.cc index ab6f3dc..319853d 100755 --- a/src/Matrix.cc +++ b/src/Matrix.cc @@ -1895,14 +1895,14 @@ public: ResizeASyncWorker(Nan::Callback *callback, Matrix *image, cv::Size size, double fx, double fy, int interpolation) : Nan::AsyncWorker(callback), image(image), + dest(NULL), size(size), fx(fx), fy(fy), interpolation(interpolation), - success(0), - dest(NULL){ + success(0) { } - + ~ResizeASyncWorker() { // don't leave this if it was allocated // could happen if NaN does not call HandleSuccess?