Merge pull request #588 from btsimonh/refcount

Refcount
This commit is contained in:
Peter Braden 2017-11-13 09:58:02 +01:00 committed by GitHub
commit fa663d449b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 70 additions and 0 deletions

27
examples/matrix-ref-count.js Executable file
View File

@ -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');
});

View File

@ -117,7 +117,10 @@ void Matrix::Init(Local<Object> target) {
Nan::SetPrototypeMethod(ctor, "mean", Mean); Nan::SetPrototypeMethod(ctor, "mean", Mean);
Nan::SetPrototypeMethod(ctor, "shift", Shift); Nan::SetPrototypeMethod(ctor, "shift", Shift);
Nan::SetPrototypeMethod(ctor, "reshape", Reshape); 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, "release", Release);
Nan::SetPrototypeMethod(ctor, "getrefCount", GetrefCount);
Nan::SetPrototypeMethod(ctor, "subtract", Subtract); Nan::SetPrototypeMethod(ctor, "subtract", Subtract);
Nan::SetPrototypeMethod(ctor, "compare", Compare); Nan::SetPrototypeMethod(ctor, "compare", Compare);
Nan::SetPrototypeMethod(ctor, "mul", Mul); Nan::SetPrototypeMethod(ctor, "mul", Mul);
@ -3027,6 +3030,42 @@ NAN_METHOD(Matrix::Release) {
return; 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<Matrix>(info.This());
// self->mat.addref();
//
// return;
//}
NAN_METHOD(Matrix::GetrefCount) {
Nan::HandleScope scope;
Matrix *self = Nan::ObjectWrap::Unwrap<Matrix>(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<Number>(refcount));
return;
}
NAN_METHOD(Matrix::Subtract) { NAN_METHOD(Matrix::Subtract) {
SETUP_FUNCTION(Matrix) SETUP_FUNCTION(Matrix)

View File

@ -134,7 +134,11 @@ public:
JSFUNC(Shift) JSFUNC(Shift)
JSFUNC(Reshape) 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(Release)
JSFUNC(GetrefCount)
JSFUNC(Subtract) JSFUNC(Subtract)
JSFUNC(Compare) JSFUNC(Compare)