From dd11d2a89a9150122a1cb55fba2b003c6d389fa0 Mon Sep 17 00:00:00 2001 From: Ian Rogers Date: Wed, 19 Nov 2014 10:06:46 -0800 Subject: Fix a memory leak in jni_internal_test. Caught by valgrind. Change-Id: I8b3cc0ce946bd457c380655d3e1237b029ed4cc3 --- runtime/jni_internal_test.cc | 115 ++++++++++++++++++++++++------------------- 1 file changed, 64 insertions(+), 51 deletions(-) (limited to 'runtime/jni_internal_test.cc') diff --git a/runtime/jni_internal_test.cc b/runtime/jni_internal_test.cc index c24ef05486..62b6b3407f 100644 --- a/runtime/jni_internal_test.cc +++ b/runtime/jni_internal_test.cc @@ -390,59 +390,72 @@ class JniInternalTest : public CommonCompilerTest { void ReleasePrimitiveArrayElementsOfWrongType(bool check_jni) { bool old_check_jni = vm_->SetCheckJniEnabled(check_jni); CheckJniAbortCatcher jni_abort_catcher; - - jbooleanArray array = env_->NewBooleanArray(10); - ASSERT_TRUE(array != nullptr); - jboolean is_copy; - jboolean* elements = env_->GetBooleanArrayElements(array, &is_copy); - ASSERT_TRUE(elements != nullptr); - env_->ReleaseByteArrayElements(reinterpret_cast(array), - reinterpret_cast(elements), 0); - jni_abort_catcher.Check( - check_jni ? "incompatible array type boolean[] expected byte[]" - : "attempt to release byte primitive array elements with an object of type boolean[]"); - env_->ReleaseShortArrayElements(reinterpret_cast(array), - reinterpret_cast(elements), 0); - jni_abort_catcher.Check( - check_jni ? "incompatible array type boolean[] expected short[]" - : "attempt to release short primitive array elements with an object of type boolean[]"); - env_->ReleaseCharArrayElements(reinterpret_cast(array), - reinterpret_cast(elements), 0); - jni_abort_catcher.Check( - check_jni ? "incompatible array type boolean[] expected char[]" - : "attempt to release char primitive array elements with an object of type boolean[]"); - env_->ReleaseIntArrayElements(reinterpret_cast(array), - reinterpret_cast(elements), 0); - jni_abort_catcher.Check( - check_jni ? "incompatible array type boolean[] expected int[]" - : "attempt to release int primitive array elements with an object of type boolean[]"); - env_->ReleaseLongArrayElements(reinterpret_cast(array), - reinterpret_cast(elements), 0); - jni_abort_catcher.Check( - check_jni ? "incompatible array type boolean[] expected long[]" - : "attempt to release long primitive array elements with an object of type boolean[]"); - env_->ReleaseFloatArrayElements(reinterpret_cast(array), - reinterpret_cast(elements), 0); - jni_abort_catcher.Check( - check_jni ? "incompatible array type boolean[] expected float[]" - : "attempt to release float primitive array elements with an object of type boolean[]"); - env_->ReleaseDoubleArrayElements(reinterpret_cast(array), - reinterpret_cast(elements), 0); - jni_abort_catcher.Check( - check_jni ? "incompatible array type boolean[] expected double[]" - : "attempt to release double primitive array elements with an object of type boolean[]"); - jbyteArray array2 = env_->NewByteArray(10); - env_->ReleaseBooleanArrayElements(reinterpret_cast(array2), elements, 0); - jni_abort_catcher.Check( - check_jni ? "incompatible array type byte[] expected boolean[]" - : "attempt to release boolean primitive array elements with an object of type byte[]"); - jobject object = env_->NewStringUTF("Test String"); - env_->ReleaseBooleanArrayElements(reinterpret_cast(object), elements, 0); - jni_abort_catcher.Check( - check_jni ? "jarray argument has non-array type: java.lang.String" - : "attempt to release boolean primitive array elements with an object of type " + { + jbooleanArray array = env_->NewBooleanArray(10); + ASSERT_TRUE(array != nullptr); + jboolean is_copy; + jboolean* elements = env_->GetBooleanArrayElements(array, &is_copy); + ASSERT_TRUE(elements != nullptr); + env_->ReleaseByteArrayElements(reinterpret_cast(array), + reinterpret_cast(elements), 0); + jni_abort_catcher.Check( + check_jni ? "incompatible array type boolean[] expected byte[]" + : "attempt to release byte primitive array elements with an object of type boolean[]"); + env_->ReleaseShortArrayElements(reinterpret_cast(array), + reinterpret_cast(elements), 0); + jni_abort_catcher.Check( + check_jni ? "incompatible array type boolean[] expected short[]" + : "attempt to release short primitive array elements with an object of type boolean[]"); + env_->ReleaseCharArrayElements(reinterpret_cast(array), + reinterpret_cast(elements), 0); + jni_abort_catcher.Check( + check_jni ? "incompatible array type boolean[] expected char[]" + : "attempt to release char primitive array elements with an object of type boolean[]"); + env_->ReleaseIntArrayElements(reinterpret_cast(array), + reinterpret_cast(elements), 0); + jni_abort_catcher.Check( + check_jni ? "incompatible array type boolean[] expected int[]" + : "attempt to release int primitive array elements with an object of type boolean[]"); + env_->ReleaseLongArrayElements(reinterpret_cast(array), + reinterpret_cast(elements), 0); + jni_abort_catcher.Check( + check_jni ? "incompatible array type boolean[] expected long[]" + : "attempt to release long primitive array elements with an object of type boolean[]"); + env_->ReleaseFloatArrayElements(reinterpret_cast(array), + reinterpret_cast(elements), 0); + jni_abort_catcher.Check( + check_jni ? "incompatible array type boolean[] expected float[]" + : "attempt to release float primitive array elements with an object of type boolean[]"); + env_->ReleaseDoubleArrayElements(reinterpret_cast(array), + reinterpret_cast(elements), 0); + jni_abort_catcher.Check( + check_jni ? "incompatible array type boolean[] expected double[]" + : "attempt to release double primitive array elements with an object of type boolean[]"); + + // Don't leak the elements array. + env_->ReleaseBooleanArrayElements(array, elements, 0); + } + { + jbyteArray array = env_->NewByteArray(10); + jboolean is_copy; + jbyte* elements = env_->GetByteArrayElements(array, &is_copy); + + env_->ReleaseBooleanArrayElements(reinterpret_cast(array), + reinterpret_cast(elements), 0); + jni_abort_catcher.Check( + check_jni ? "incompatible array type byte[] expected boolean[]" + : "attempt to release boolean primitive array elements with an object of type byte[]"); + jobject object = env_->NewStringUTF("Test String"); + env_->ReleaseBooleanArrayElements(reinterpret_cast(object), + reinterpret_cast(elements), 0); + jni_abort_catcher.Check( + check_jni ? "jarray argument has non-array type: java.lang.String" + : "attempt to release boolean primitive array elements with an object of type " "java.lang.String"); + // Don't leak the elements array. + env_->ReleaseByteArrayElements(array, elements, 0); + } EXPECT_EQ(check_jni, vm_->SetCheckJniEnabled(old_check_jni)); } -- cgit v1.2.3