Skip to content

Commit

Permalink
[xgb] Fix xgb intern to close replaced array (#3558)
Browse files Browse the repository at this point in the history
* [xgb] Fix xgb intern to close replaced array

* [xgb] Fix xgb intern to close replaced array

* Review changes

* Review changes

---------

Co-authored-by: Xin Yang <xyang19@gmail.com>
  • Loading branch information
ewan0x79 and xyang16 authored Jan 22, 2025
1 parent a4e61ea commit 0ea9091
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
2 changes: 1 addition & 1 deletion api/src/main/java/ai/djl/ndarray/NDArrayAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public abstract class NDArrayAdapter implements NDArray {

protected NDManager manager;
protected NDManager alternativeManager;
private NDArray alternativeArray;
protected NDArray alternativeArray;

protected Shape shape;
protected DataType dataType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,33 @@ public ByteBuffer toByteBuffer(boolean tryDirect) {
/** {@inheritDoc} */
@Override
public void intern(NDArray replaced) {
if (!(replaced instanceof XgbNDArray)) {
throw new IllegalArgumentException(
"The replaced NDArray must be an instance of XgbNDArray.");
}
XgbNDArray array = (XgbNDArray) replaced;
if (isReleased()) {
throw new IllegalArgumentException("This array is already closed");
}
if (replaced.isReleased()) {
throw new IllegalArgumentException("This target array is already closed");
}

if (handle != null && handle.get() != 0L) {
long pointer = handle.getAndSet(0L);
JniUtils.deleteDMatrix(pointer);
}
XgbNDArray array = (XgbNDArray) replaced;
if (alternativeArray != null) {
alternativeArray.close();
}

data = array.data;
handle = array.handle;
format = array.format;
alternativeArray = array.alternativeArray;
array.handle = null;
array.alternativeArray = null;
array.close();
}

/** {@inheritDoc} */
Expand Down

0 comments on commit 0ea9091

Please sign in to comment.