Skip to content

Commit b87e8c7

Browse files
author
Elmi Ahmadov
committed
Remove Weak object reference from v8WeakReference array when released
V8 uses another array called v8WeakReferences to keep the weak objects and this array is used to get object reference count. Thus, when weak object is released it should also be removed from v8WeakReferences array. Fix #347
1 parent 3bf897c commit b87e8c7

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

src/main/java/com/eclipsesource/v8/V8Value.java

+6-4
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ abstract public class V8Value implements Releasable {
4848
public static final int FLOAT_32_ARRAY = 16;
4949
public static final int UNDEFINED = 99;
5050

51-
protected V8 v8;
52-
protected long objectHandle;
53-
protected boolean released = true;
51+
protected V8 v8;
52+
protected long objectHandle;
53+
protected boolean released = true;
5454

5555
protected V8Value() {
5656
super();
@@ -83,7 +83,6 @@ protected void addObjectReference(final long objectHandle) throws Error {
8383
}
8484
}
8585

86-
8786
/**
8887
* Returns a string representation of the V8 Type.
8988
* @param type Type to return as a string. See constants in V8Value.
@@ -261,6 +260,9 @@ public void close() {
261260
v8.checkThread();
262261
if (!released) {
263262
try {
263+
if (isWeak()) {
264+
v8.v8WeakReferences.remove(getHandle());
265+
}
264266
v8.releaseObjRef(this);
265267
} finally {
266268
released = true;

src/test/java/com/eclipsesource/v8/V8ObjectTest.java

+10-2
Original file line numberDiff line numberDiff line change
@@ -1317,7 +1317,7 @@ public void testGetTypeFunction() {
13171317
public void testGetKeysOnObject() {
13181318
V8Object v8Object = new V8Object(v8);
13191319
v8Object.add("integer", 1).add("double", 1.1).add("boolean", true)
1320-
.add("string", "hello, world!");
1320+
.add("string", "hello, world!");
13211321

13221322
String[] keys = v8Object.getKeys();
13231323

@@ -1842,11 +1842,19 @@ public void testSetWeakMakesObjectWeak() {
18421842

18431843
@SuppressWarnings("resource")
18441844
@Test
1845-
public void testClearWeakMakesObjectWeak() {
1845+
public void testClearWeakMakesObjectNonWeak() {
18461846
V8Value object = new V8Object(v8).setWeak().clearWeak();
18471847

18481848
assertFalse(object.isWeak());
18491849
object.close();
18501850
}
18511851

1852+
@SuppressWarnings("resource")
1853+
@Test
1854+
public void testReleaseWeakObjectDoesNotAffectReferenceCount() {
1855+
new V8Object(v8).setWeak().close();
1856+
1857+
assertEquals(0, v8.getObjectReferenceCount());
1858+
}
1859+
18521860
}

0 commit comments

Comments
 (0)