mirror of
https://github.com/peterbraden/node-opencv.git
synced 2025-12-08 19:45:55 +00:00
opencv:readimage Async - change newed mat to local class to try to prevent mem leak; also try-catch to signal failure through callback
This commit is contained in:
parent
fdd0c97a3c
commit
6d09f70367
@ -24,30 +24,54 @@ public:
|
|||||||
Nan::AsyncWorker(callback),
|
Nan::AsyncWorker(callback),
|
||||||
buf(buf),
|
buf(buf),
|
||||||
len(len),
|
len(len),
|
||||||
flags(flags){
|
flags(flags),
|
||||||
|
success(0){
|
||||||
}
|
}
|
||||||
|
|
||||||
~AsyncImDecodeWorker() {
|
~AsyncImDecodeWorker() {
|
||||||
}
|
}
|
||||||
|
|
||||||
void Execute() {
|
void Execute() {
|
||||||
// Local<Object> img_to_return =
|
try{
|
||||||
// Nan::NewInstance(Nan::GetFunction(Nan::New(Matrix::constructor)).ToLocalChecked()).ToLocalChecked();
|
// don't new; just have a local class which will be removed.
|
||||||
// img = Nan::ObjectWrap::Unwrap<Matrix>(img_to_return);
|
cv::Mat mbuf(len, 1, CV_64FC1, buf);
|
||||||
cv::Mat *mbuf = new cv::Mat(len, 1, CV_64FC1, buf);
|
outputmat = cv::imdecode(mbuf, flags);
|
||||||
outputmat = cv::imdecode(*mbuf, flags);
|
success = 1;
|
||||||
|
} catch(...){
|
||||||
|
success = 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void HandleOKCallback() {
|
void HandleOKCallback() {
|
||||||
Nan::HandleScope scope;
|
|
||||||
|
|
||||||
Local<Object> im_to_return= Nan::NewInstance(Nan::GetFunction(Nan::New(Matrix::constructor)).ToLocalChecked()).ToLocalChecked();
|
if (success){
|
||||||
Matrix *img = Nan::ObjectWrap::Unwrap<Matrix>(im_to_return);
|
Nan::HandleScope scope;
|
||||||
img->mat = outputmat;
|
|
||||||
|
|
||||||
|
try{
|
||||||
|
Local<Object> im_to_return= Nan::NewInstance(Nan::GetFunction(Nan::New(Matrix::constructor)).ToLocalChecked()).ToLocalChecked();
|
||||||
|
Matrix *img = Nan::ObjectWrap::Unwrap<Matrix>(im_to_return);
|
||||||
|
img->mat = outputmat;
|
||||||
|
|
||||||
|
Local<Value> argv[] = {
|
||||||
|
Nan::Null(),
|
||||||
|
im_to_return
|
||||||
|
};
|
||||||
|
|
||||||
|
Nan::TryCatch try_catch;
|
||||||
|
callback->Call(2, argv);
|
||||||
|
if (try_catch.HasCaught()) {
|
||||||
|
Nan::FatalException(try_catch);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
} catch (...){
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// fall through here is !success or failed trying to callback.
|
||||||
Local<Value> argv[] = {
|
Local<Value> argv[] = {
|
||||||
Nan::Null(),
|
Nan::New("C++ exception executing imdecode").ToLocalChecked(), // err
|
||||||
im_to_return
|
Nan::Null()
|
||||||
};
|
};
|
||||||
|
|
||||||
Nan::TryCatch try_catch;
|
Nan::TryCatch try_catch;
|
||||||
@ -62,6 +86,7 @@ private:
|
|||||||
unsigned len;
|
unsigned len;
|
||||||
int flags;
|
int flags;
|
||||||
cv::Mat outputmat;
|
cv::Mat outputmat;
|
||||||
|
int success;
|
||||||
//Matrix *img;
|
//Matrix *img;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user