diff --git a/examples/matrix-ref-count.js b/examples/matrix-ref-count.js new file mode 100755 index 0000000..cd0ad08 --- /dev/null +++ b/examples/matrix-ref-count.js @@ -0,0 +1,27 @@ +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'); + + + // 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.release(); + var refcount3 = im.getrefCount(); + console.log('refcount after release (seems should be -1) '+refcount3); + if (refcount3 !== -1) + throw "refcountmismatch - after release should be -1"; + + // 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'); +}); diff --git a/src/Matrix.cc b/src/Matrix.cc index c2f2dc0..2c4206c 100755 --- a/src/Matrix.cc +++ b/src/Matrix.cc @@ -117,7 +117,10 @@ void Matrix::Init(Local target) { Nan::SetPrototypeMethod(ctor, "mean", Mean); Nan::SetPrototypeMethod(ctor, "shift", Shift); Nan::SetPrototypeMethod(ctor, "reshape", Reshape); +// 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); Nan::SetPrototypeMethod(ctor, "compare", Compare); Nan::SetPrototypeMethod(ctor, "mul", Mul); @@ -3027,6 +3030,42 @@ NAN_METHOD(Matrix::Release) { 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) { + 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; // indicates no reference ptr + } +#else + if (self->mat.refcount){ + refcount = *(self->mat.refcount); + } else { + refcount = -1; // indicates no reference ptr + } +#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..81e89b2 100755 --- a/src/Matrix.h +++ b/src/Matrix.h @@ -134,7 +134,11 @@ public: JSFUNC(Shift) JSFUNC(Reshape) + +// 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) JSFUNC(Subtract) JSFUNC(Compare)