Fix YogaLogger leakage when removing it from YogaConfig

Summary: -

Reviewed By: emilsjolander

Differential Revision: D5802035

fbshipit-source-id: 79c1f8c3cc5ddf3abd51206f23e076f2410cfc0c
This commit is contained in:
Amir Shalem
2017-09-11 02:59:15 -07:00
committed by Facebook Github Bot
parent c20f2864ab
commit 16052085d0
2 changed files with 44 additions and 3 deletions

View File

@@ -422,9 +422,7 @@ void jni_YGConfigSetLogger(alias_ref<jobject>, jlong nativePointer, alias_ref<jo
auto context = YGConfigGetContext(config);
if (context) {
auto jlogger = reinterpret_cast<global_ref<jobject> *>(context);
jlogger->releaseAlias();
delete jlogger;
delete reinterpret_cast<global_ref<jobject> *>(context);
}
if (logger) {

View File

@@ -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<Object> ref = new WeakReference<Object>(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");
}
}