bytedeco / javacpp

The missing bridge between Java and native C++
Other
4.46k stars 581 forks source link

retain and release method of DeallocatorReference #702

Closed chenyexin2012 closed 11 months ago

chenyexin2012 commented 1 year ago

when I only call the retain method once, the count value becomes 1, and then call the release method, (deallocator != null && count.decrementAndGet() <= 0) will be true, and the deallocate method will be executed. in this case, it seems useless to call the retain method only once

@Override 
public void retain() {
    if (deallocator != null) {
        count.incrementAndGet();
    }
}
@Override 
public boolean release() {
    if (deallocator != null && count.decrementAndGet() <= 0) {
        if (logger.isDebugEnabled()) {
            logger.debug("Releasing " + this);
        }
        deallocate();
        return true;
    }
    return false;
}

i think it's more suitable to use getAndDecrement method to instead decrementAndGet.

saudet commented 1 year ago

Could you explain the advantage of the method you're proposing?

chenyexin2012 commented 1 year ago
Mat mat = new Mat(new Size(3,3), CV_8U, Scalar.all(0));
// increase the reference count by 1
mat.retainReference();
// mat will be released
// it seems useless to call the retainReference method only one time
mat.releaseReference();
// i don't understand why it's designed like this

mat = new Mat(new Size(3,3), CV_8U, Scalar.all(0));
// call retainReference twice
mat.retainReference();
mat.retainReference();
// mat will not be released
mat.releaseReference();
saudet commented 1 year ago

Maybe what you're looking for is the close() method, which always deallocates?

saudet commented 12 months ago

If you're just looking for an explanation, I talk here a bit about the reasons why it's like that: https://groups.google.com/a/tensorflow.org/g/jvm/c/FyXjlFmmTJI/m/NeDG-OC6AQAJ

chenyexin2012 commented 11 months ago

If you're just looking for an explanation, I talk here a bit about the reasons why it's like that: https://groups.google.com/a/tensorflow.org/g/jvm/c/FyXjlFmmTJI/m/NeDG-OC6AQAJ

I see, thank you!