Skip to content
This repository was archived by the owner on Jan 4, 2023. It is now read-only.

Commit de8304c

Browse files
Tzafrir Poupkopetreeftime
authored andcommitted
Provide a java method to release native memory
Calling close allows java code to release the native memory associated with the bluetooth object. This provides control over the allocated memory, without the need to wait for the finalize method to be invoked by the JVM. Signed-off-by: Tzafrir Poupko <[email protected]>
1 parent b3c053c commit de8304c

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

java/BluetoothObject.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@
2626

2727
import java.util.*;
2828

29-
public abstract class BluetoothObject implements Cloneable
29+
public abstract class BluetoothObject implements Cloneable,AutoCloseable
3030
{
3131
protected long nativeInstance;
32+
private boolean isValid;
3233

3334
static {
3435
try {
@@ -57,11 +58,12 @@ public abstract class BluetoothObject implements Cloneable
5758
protected BluetoothObject(long instance)
5859
{
5960
nativeInstance = instance;
61+
isValid = true;
6062
}
6163

6264
protected void finalize()
6365
{
64-
delete();
66+
close();
6567
}
6668

6769
public boolean equals(Object obj)
@@ -77,4 +79,16 @@ public int hashCode() {
7779
String objectPath = getObjectPath();
7880
return objectPath.hashCode();
7981
}
82+
83+
/**
84+
* Release the native memory associated with this object
85+
* The object should not be used following a call to close
86+
*/
87+
@Override
88+
public synchronized void close() {
89+
if (!isValid)
90+
return;
91+
isValid = false;
92+
delete();
93+
}
8094
}

0 commit comments

Comments
 (0)