Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Fix crash in OpenCV::ReadImageAsync
Fix occasional segfault when running examples/readimage.js
  • Loading branch information
Jochen Sprickerhof authored and kapouer committed Apr 30, 2021
commit bf0114f7b2e70c032211abe53eadea52ba2aebe0
5 changes: 4 additions & 1 deletion src/OpenCV.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class AsyncImDecodeWorker: public Nan::AsyncWorker {
cv::Mat mbuf(len, 1, CV_64FC1, buf);
outputmat = cv::imdecode(mbuf, flags);
success = 1;
free(buf);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will leak memory if an exception is thrown earlier

} catch(...){
success = 0;
}
Expand Down Expand Up @@ -224,8 +225,10 @@ NAN_METHOD(OpenCV::ReadImageAsync) {
// async
uint8_t *buf = (uint8_t *) Buffer::Data(Nan::To<v8::Object>(info[0]).ToLocalChecked());
unsigned len = Buffer::Length(Nan::To<v8::Object>(info[0]).ToLocalChecked());
uint8_t *buf_new = (uint8_t *) malloc(len);
memcpy(buf_new, buf, len);
Nan::Callback *callback = new Nan::Callback(cb.As<Function>());
Nan::AsyncQueueWorker(new AsyncImDecodeWorker(callback, buf, len, flags));
Nan::AsyncQueueWorker(new AsyncImDecodeWorker(callback, buf_new, len, flags));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of copying the buffer to mitigate premature garbage collection, it would be better to pass the object itself and make sure that it won't be garbage collected until AsyncImDecodeWorker has completed.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay but i don't know how to do that.

return;
}
// WILL have returned by here unless exception
Expand Down