Fix a bug in video_encoder_wrapper where int array was not freed properly.

JNI_COMMIT doesn't actually free the buffer.

From JNI docs:
0: copy back the content and free the elems buffer
JNI_COMMIT: copy back the content but do not free the elems buffer
JNI_ABORT: free the buffer without copying back the possible changes

Also introduces helper methods to help avoid this problem in the
future.

Bug: webrtc:10132
Change-Id: I769df286d3bd186fdf39ee2363e9002f36454509
Reviewed-on: https://webrtc-review.googlesource.com/c/120600
Reviewed-by: Magnus Jedvert <magjed@webrtc.org>
Commit-Queue: Sami Kalliomäki <sakal@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#26529}
This commit is contained in:
Sami Kalliomäki
2019-02-04 13:42:11 +01:00
committed by Commit Bot
parent eee110dea2
commit ee61f9440a
7 changed files with 110 additions and 21 deletions

View File

@ -30,6 +30,47 @@ TEST(JavaTypesTest, TestJavaToNativeStringMap) {
};
EXPECT_EQ(expected, output);
}
TEST(JavaTypesTest, TestNativeToJavaToNativeIntArray) {
JNIEnv* env = AttachCurrentThreadIfNeeded();
std::vector<int32_t> test_data{1, 20, 300};
ScopedJavaLocalRef<jintArray> array = NativeToJavaIntArray(env, test_data);
EXPECT_EQ(test_data, JavaToNativeIntArray(env, array));
}
TEST(JavaTypesTest, TestNativeToJavaToNativeByteArray) {
JNIEnv* env = AttachCurrentThreadIfNeeded();
std::vector<int8_t> test_data{1, 20, 30};
ScopedJavaLocalRef<jbyteArray> array = NativeToJavaByteArray(env, test_data);
EXPECT_EQ(test_data, JavaToNativeByteArray(env, array));
}
TEST(JavaTypesTest, TestNativeToJavaToNativeIntArrayLeakTest) {
JNIEnv* env = AttachCurrentThreadIfNeeded();
std::vector<int32_t> test_data{1, 20, 300};
for (int i = 0; i < 2000; i++) {
ScopedJavaLocalRef<jintArray> array = NativeToJavaIntArray(env, test_data);
EXPECT_EQ(test_data, JavaToNativeIntArray(env, array));
}
}
TEST(JavaTypesTest, TestNativeToJavaToNativeByteArrayLeakTest) {
JNIEnv* env = AttachCurrentThreadIfNeeded();
std::vector<int8_t> test_data{1, 20, 30};
for (int i = 0; i < 2000; i++) {
ScopedJavaLocalRef<jbyteArray> array =
NativeToJavaByteArray(env, test_data);
EXPECT_EQ(test_data, JavaToNativeByteArray(env, array));
}
}
} // namespace
} // namespace test
} // namespace webrtc