Merge pull request #583 from btsimonh/resizeexceptionchecking

Resize (async) exception checking and memory leak prevention
This commit is contained in:
btsimonh 2017-11-10 16:16:23 +00:00 committed by GitHub
commit 65b64b162d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1895,14 +1895,19 @@ public:
ResizeASyncWorker(Nan::Callback *callback, Matrix *image, cv::Size size, double fx, double fy, int interpolation) : ResizeASyncWorker(Nan::Callback *callback, Matrix *image, cv::Size size, double fx, double fy, int interpolation) :
Nan::AsyncWorker(callback), Nan::AsyncWorker(callback),
image(image), image(image),
dest(NULL),
size(size), size(size),
fx(fx), fx(fx),
fy(fy), fy(fy),
interpolation(interpolation), interpolation(interpolation),
success(0){ success(0) {
} }
~ResizeASyncWorker() { ~ResizeASyncWorker() {
// don't leave this if it was allocated
// could happen if NaN does not call HandleSuccess?
delete dest;
dest = NULL;
} }
void Execute() { void Execute() {
@ -1919,12 +1924,12 @@ public:
Nan::HandleScope scope; Nan::HandleScope scope;
if (success){ if (success){
try{
Local<Object> im_to_return= Nan::NewInstance(Nan::GetFunction(Nan::New(Matrix::constructor)).ToLocalChecked()).ToLocalChecked(); Local<Object> im_to_return= Nan::NewInstance(Nan::GetFunction(Nan::New(Matrix::constructor)).ToLocalChecked()).ToLocalChecked();
Matrix *img = Nan::ObjectWrap::Unwrap<Matrix>(im_to_return); Matrix *img = Nan::ObjectWrap::Unwrap<Matrix>(im_to_return);
img->mat = dest->mat; img->mat = dest->mat;
delete dest; delete dest;
dest = NULL;
//delete dest;
Local<Value> argv[] = { Local<Value> argv[] = {
Nan::Null(), // err Nan::Null(), // err
@ -1936,8 +1941,23 @@ public:
if (try_catch.HasCaught()) { if (try_catch.HasCaught()) {
Nan::FatalException(try_catch); Nan::FatalException(try_catch);
} }
} catch (...){
delete dest;
dest = NULL;
Local<Value> 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 { } else {
delete dest; delete dest;
dest = NULL;
Local<Value> argv[] = { Local<Value> argv[] = {
Nan::New("C++ exception").ToLocalChecked(), // err Nan::New("C++ exception").ToLocalChecked(), // err
@ -2022,11 +2042,15 @@ NAN_METHOD(Matrix::Resize) {
Nan::AsyncQueueWorker(new ResizeASyncWorker(callback, self, size, fx, fy, interpolation)); Nan::AsyncQueueWorker(new ResizeASyncWorker(callback, self, size, fx, fy, interpolation));
info.GetReturnValue().Set(Nan::Null()); info.GetReturnValue().Set(Nan::Null());
} else { } else {
try{
Matrix *self = Nan::ObjectWrap::Unwrap<Matrix>(info.This()); Matrix *self = Nan::ObjectWrap::Unwrap<Matrix>(info.This());
cv::Mat res = cv::Mat(x, y, CV_32FC3); cv::Mat res = cv::Mat(x, y, CV_32FC3);
cv::resize(self->mat, res, cv::Size(x, y), 0, 0, interpolation); cv::resize(self->mat, res, cv::Size(x, y), 0, 0, interpolation);
~self->mat; ~self->mat;
self->mat = res; self->mat = res;
} catch (...){
return Nan::ThrowError("c++ Exception processing resize");
}
} }
} }