diff --git a/java/jni/YGJNI.cpp b/java/jni/YGJNI.cpp index e4dc0bb5..df039883 100644 --- a/java/jni/YGJNI.cpp +++ b/java/jni/YGJNI.cpp @@ -422,9 +422,7 @@ void jni_YGConfigSetLogger(alias_ref, jlong nativePointer, alias_ref *>(context); - jlogger->releaseAlias(); - delete jlogger; + delete reinterpret_cast *>(context); } if (logger) { diff --git a/java/tests/com/facebook/yoga/YogaLoggerTest.java b/java/tests/com/facebook/yoga/YogaLoggerTest.java new file mode 100644 index 00000000..eab4e7ae --- /dev/null +++ b/java/tests/com/facebook/yoga/YogaLoggerTest.java @@ -0,0 +1,43 @@ +/** + * Copyright (c) 2014-present, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + */ + +package com.facebook.yoga; + +import org.junit.Test; +import java.lang.ref.WeakReference; + +import static org.junit.Assert.fail; + +public class YogaLoggerTest { + @Test + public void testLoggerLeak() throws Exception { + final YogaConfig config = new YogaConfig(); + YogaLogger logger = new YogaLogger() { + @Override + public void log(YogaNode yogaNode, YogaLogLevel level, String message) { + } + }; + config.setLogger(logger); + config.setLogger(null); + WeakReference ref = new WeakReference(logger); + // noinspection UnusedAssignment + logger = null; + // try and free for the next 5 seconds, usually it works after the + // first GC attempt. + for (int i=0; i < 50; i++) { + System.gc(); + if (ref.get() == null) { + // free successfully + return; + } + Thread.sleep(100); + } + fail("YogaLogger leaked"); + } +}