From 96d0eb88241b9b7cf8e57ab800e39411871ad9ab Mon Sep 17 00:00:00 2001 From: Simon Hailes Date: Sun, 12 Nov 2017 16:03:17 +0000 Subject: [PATCH 1/5] Matrix: add mtx.addref() and mtx.getrefCount() --- src/Matrix.cc | 37 +++++++++++++++++++++++++++++++++++++ src/Matrix.h | 3 +++ 2 files changed, 40 insertions(+) diff --git a/src/Matrix.cc b/src/Matrix.cc index 319853d..70c79fa 100755 --- a/src/Matrix.cc +++ b/src/Matrix.cc @@ -117,7 +117,9 @@ void Matrix::Init(Local target) { Nan::SetPrototypeMethod(ctor, "mean", Mean); Nan::SetPrototypeMethod(ctor, "shift", Shift); Nan::SetPrototypeMethod(ctor, "reshape", Reshape); + Nan::SetPrototypeMethod(ctor, "addref", Addref); Nan::SetPrototypeMethod(ctor, "release", Release); + Nan::SetPrototypeMethod(ctor, "getrefCount", GetrefCount); Nan::SetPrototypeMethod(ctor, "subtract", Subtract); Nan::SetPrototypeMethod(ctor, "compare", Compare); Nan::SetPrototypeMethod(ctor, "mul", Mul); @@ -3025,6 +3027,41 @@ NAN_METHOD(Matrix::Release) { return; } +NAN_METHOD(Matrix::Addref) { + Nan::HandleScope scope; + + Matrix *self = Nan::ObjectWrap::Unwrap(info.This()); + self->mat.addref(); + + return; +} + + +NAN_METHOD(Matrix::GetrefCount) { + Nan::HandleScope scope; + Matrix *self = Nan::ObjectWrap::Unwrap(info.This()); + + int refcount = -1; + +#if CV_MAJOR_VERSION >= 3 + if (self->mat.u){ + refcount = self->mat.u->refcount; + } else { + refcount = -1; + } +#else + if (self->mat.refcount){ + refcount = *self->mat.refcount); + } else { + refcount = -1; + } +#endif + + info.GetReturnValue().Set(Nan::New(refcount)); + return; +} + + NAN_METHOD(Matrix::Subtract) { SETUP_FUNCTION(Matrix) diff --git a/src/Matrix.h b/src/Matrix.h index 5033c61..e904344 100755 --- a/src/Matrix.h +++ b/src/Matrix.h @@ -134,7 +134,10 @@ public: JSFUNC(Shift) JSFUNC(Reshape) + + JSFUNC(Addref) JSFUNC(Release) + JSFUNC(GetrefCount) JSFUNC(Subtract) JSFUNC(Compare) From 9316a01f031f238ae13bd6f24e31d1cc714d9cc3 Mon Sep 17 00:00:00 2001 From: Simon Hailes Date: Sun, 12 Nov 2017 16:48:08 +0000 Subject: [PATCH 2/5] fix typo --- src/Matrix.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Matrix.cc b/src/Matrix.cc index 70c79fa..a282e4d 100755 --- a/src/Matrix.cc +++ b/src/Matrix.cc @@ -3051,7 +3051,7 @@ NAN_METHOD(Matrix::GetrefCount) { } #else if (self->mat.refcount){ - refcount = *self->mat.refcount); + refcount = *(self->mat.refcount); } else { refcount = -1; } From 2c1732f0c4cd87be83d4c9b7721cbc5a3cb05d42 Mon Sep 17 00:00:00 2001 From: Simon Hailes Date: Sun, 12 Nov 2017 17:38:01 +0000 Subject: [PATCH 3/5] add example matrix-ref-count --- examples/matrix-ref-count.js | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100755 examples/matrix-ref-count.js diff --git a/examples/matrix-ref-count.js b/examples/matrix-ref-count.js new file mode 100755 index 0000000..2540f5d --- /dev/null +++ b/examples/matrix-ref-count.js @@ -0,0 +1,35 @@ +var cv = require('../lib/opencv'); + +cv.readImage('./files/mona.png', function(err, im) { + if (err) throw err; + if (im.width() < 1 || im.height() < 1) throw new Error('Image has no size'); + + var refcount = im.getrefCount(); + console.log('initial refcount '+refcount); + + im.addref(); + var refcount2 = im.getrefCount(); + console.log('refcount after addref '+refcount2); + if (refcount2 !== (refcount + 1)) + throw "refcountmismatch"; + + im.addref(); + refcount2 = im.getrefCount(); + console.log('refcount after addref 2 '+refcount2); + if (refcount2 !== (refcount + 2)) + throw "refcountmismatch"; + + + im.release(); + var refcount3 = im.getrefCount(); + console.log('refcount after release (seems should be -1) '+refcount3); + if (refcount3 !== -1) + throw "refcountmismatch"; + + // data is now still there somewhere, but lost to us - this is NOT a good situation. + + // should not fail - but will do absolutely nothing + im.release(); + + console.log('did all refcount tests'); +}); From bc8095534b2954ef544bc8e4a7b5e0d219f75d71 Mon Sep 17 00:00:00 2001 From: Simon Hailes Date: Sun, 12 Nov 2017 17:48:36 +0000 Subject: [PATCH 4/5] comment out Addref - too dangerous to leave in. Modify test example. --- examples/matrix-ref-count.js | 20 ++++++-------------- src/Matrix.cc | 24 +++++++++++++----------- 2 files changed, 19 insertions(+), 25 deletions(-) diff --git a/examples/matrix-ref-count.js b/examples/matrix-ref-count.js index 2540f5d..cd0ad08 100755 --- a/examples/matrix-ref-count.js +++ b/examples/matrix-ref-count.js @@ -4,27 +4,19 @@ cv.readImage('./files/mona.png', function(err, im) { if (err) throw err; if (im.width() < 1 || im.height() < 1) throw new Error('Image has no size'); + + // getrefCount will give the refcount. if > 0, then a reference ptr is present, and the value returned. + // if -1, no reference pointer was present (i.e. the mat does not know about any data). var refcount = im.getrefCount(); console.log('initial refcount '+refcount); + if (refcount !== 1) + throw "refcountmismatch - initial should be 1"; - im.addref(); - var refcount2 = im.getrefCount(); - console.log('refcount after addref '+refcount2); - if (refcount2 !== (refcount + 1)) - throw "refcountmismatch"; - - im.addref(); - refcount2 = im.getrefCount(); - console.log('refcount after addref 2 '+refcount2); - if (refcount2 !== (refcount + 2)) - throw "refcountmismatch"; - - im.release(); var refcount3 = im.getrefCount(); console.log('refcount after release (seems should be -1) '+refcount3); if (refcount3 !== -1) - throw "refcountmismatch"; + throw "refcountmismatch - after release should be -1"; // data is now still there somewhere, but lost to us - this is NOT a good situation. diff --git a/src/Matrix.cc b/src/Matrix.cc index a282e4d..653de26 100755 --- a/src/Matrix.cc +++ b/src/Matrix.cc @@ -117,7 +117,8 @@ void Matrix::Init(Local target) { Nan::SetPrototypeMethod(ctor, "mean", Mean); Nan::SetPrototypeMethod(ctor, "shift", Shift); Nan::SetPrototypeMethod(ctor, "reshape", Reshape); - Nan::SetPrototypeMethod(ctor, "addref", Addref); +// leave this out - can't see a way it could be useful to us, as release() always completely forgets the data +// Nan::SetPrototypeMethod(ctor, "addref", Addref); Nan::SetPrototypeMethod(ctor, "release", Release); Nan::SetPrototypeMethod(ctor, "getrefCount", GetrefCount); Nan::SetPrototypeMethod(ctor, "subtract", Subtract); @@ -3027,14 +3028,15 @@ NAN_METHOD(Matrix::Release) { return; } -NAN_METHOD(Matrix::Addref) { - Nan::HandleScope scope; - - Matrix *self = Nan::ObjectWrap::Unwrap(info.This()); - self->mat.addref(); - - return; -} +// leave this out - can't see a way it could be useful to us, as release() always completely forgets the data +//NAN_METHOD(Matrix::Addref) { +// Nan::HandleScope scope; +// +// Matrix *self = Nan::ObjectWrap::Unwrap(info.This()); +// self->mat.addref(); +// +// return; +//} NAN_METHOD(Matrix::GetrefCount) { @@ -3047,13 +3049,13 @@ NAN_METHOD(Matrix::GetrefCount) { if (self->mat.u){ refcount = self->mat.u->refcount; } else { - refcount = -1; + refcount = -1; // indicates no reference ptr } #else if (self->mat.refcount){ refcount = *(self->mat.refcount); } else { - refcount = -1; + refcount = -1; // indicates no reference ptr } #endif From f4d0459fa791a046ccc2c0ff4435993263ba7d1e Mon Sep 17 00:00:00 2001 From: Simon Hailes Date: Mon, 13 Nov 2017 08:40:44 +0000 Subject: [PATCH 5/5] kill ref to addref in Matrix.h --- src/Matrix.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Matrix.h b/src/Matrix.h index e904344..81e89b2 100755 --- a/src/Matrix.h +++ b/src/Matrix.h @@ -135,7 +135,8 @@ public: JSFUNC(Reshape) - JSFUNC(Addref) +// leave this out - can't see a way it could be useful to us, as release() always completely forgets the data +//JSFUNC(Addref) JSFUNC(Release) JSFUNC(GetrefCount)