Skip to content

Commit

Permalink
Remove Weak object reference from v8WeakReference array when released
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Elmi Ahmadov committed Jan 18, 2019
1 parent 3bf897c commit b87e8c7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
10 changes: 6 additions & 4 deletions src/main/java/com/eclipsesource/v8/V8Value.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ abstract public class V8Value implements Releasable {
public static final int FLOAT_32_ARRAY = 16;
public static final int UNDEFINED = 99;

protected V8 v8;
protected long objectHandle;
protected boolean released = true;
protected V8 v8;
protected long objectHandle;
protected boolean released = true;

protected V8Value() {
super();
Expand Down Expand Up @@ -83,7 +83,6 @@ protected void addObjectReference(final long objectHandle) throws Error {
}
}


/**
* Returns a string representation of the V8 Type.
* @param type Type to return as a string. See constants in V8Value.
Expand Down Expand Up @@ -261,6 +260,9 @@ public void close() {
v8.checkThread();
if (!released) {
try {
if (isWeak()) {
v8.v8WeakReferences.remove(getHandle());
}
v8.releaseObjRef(this);
} finally {
released = true;
Expand Down
12 changes: 10 additions & 2 deletions src/test/java/com/eclipsesource/v8/V8ObjectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1317,7 +1317,7 @@ public void testGetTypeFunction() {
public void testGetKeysOnObject() {
V8Object v8Object = new V8Object(v8);
v8Object.add("integer", 1).add("double", 1.1).add("boolean", true)
.add("string", "hello, world!");
.add("string", "hello, world!");

String[] keys = v8Object.getKeys();

Expand Down Expand Up @@ -1842,11 +1842,19 @@ public void testSetWeakMakesObjectWeak() {

@SuppressWarnings("resource")
@Test
public void testClearWeakMakesObjectWeak() {
public void testClearWeakMakesObjectNonWeak() {
V8Value object = new V8Object(v8).setWeak().clearWeak();

assertFalse(object.isWeak());
object.close();
}

@SuppressWarnings("resource")
@Test
public void testReleaseWeakObjectDoesNotAffectReferenceCount() {
new V8Object(v8).setWeak().close();

assertEquals(0, v8.getObjectReferenceCount());
}

}

0 comments on commit b87e8c7

Please sign in to comment.