Skip to content

Commit 7a99cef

Browse files
committed
Fix {Get,Release}ArrayElements
1 parent b12eaa7 commit 7a99cef

File tree

3 files changed

+24
-8
lines changed

3 files changed

+24
-8
lines changed

include/jni/functions.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -429,13 +429,13 @@ namespace jni
429429
std::tuple<UniqueArrayElements<E>, jboolean> GetArrayElements(JNIEnv& env, jarray<E>& array)
430430
{
431431
::jboolean isCopy = JNI_FALSE;
432-
const E** result = CheckJavaException(env,
432+
E* result = CheckJavaException(env,
433433
(env.*(TypedMethods<E>::GetArrayElements))(Unwrap(array), &isCopy));
434434
return std::make_tuple(UniqueArrayElements<E>(result, ArrayElementsDeleter<E>(env, array)), Wrap<jboolean>(isCopy));
435435
}
436436

437437
template < class E >
438-
void ReleaseArrayElements(JNIEnv& env, jarray<E>& array, E** elems)
438+
void ReleaseArrayElements(JNIEnv& env, jarray<E>& array, E* elems)
439439
{
440440
(env.*(TypedMethods<E>::ReleaseArrayElements))(Unwrap(array), elems, JNI_COMMIT);
441441
CheckJavaException(env);

include/jni/ownership.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ namespace jni
135135
ArrayElementsDeleter() = default;
136136
ArrayElementsDeleter(JNIEnv& e, jarray<E>& a) : env(&e), array(&a) {}
137137

138-
void operator()(E** p) const
138+
void operator()(E* p) const
139139
{
140140
if (p)
141141
{
@@ -147,7 +147,7 @@ namespace jni
147147
};
148148

149149
template < class E >
150-
using UniqueArrayElements = std::unique_ptr< E*, ArrayElementsDeleter<E> >;
150+
using UniqueArrayElements = std::unique_ptr< E, ArrayElementsDeleter<E> >;
151151

152152

153153
template < class E >

test/low_level.cpp

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,25 @@ static void TestGetArrayLength()
115115
assert(Throws<jni::PendingJavaException>([] { jni::GetArrayLength(env, failureValue.Ref()); }));
116116
}
117117

118+
static void TestArrayElements()
119+
{
120+
static Testable<jni::jarray<jni::jboolean>> arrayValue;
121+
static TestEnv env;
122+
123+
env.functions->GetBooleanArrayElements = [] (JNIEnv*, jbooleanArray, jboolean*) -> jboolean*
124+
{
125+
return nullptr;
126+
};
127+
128+
env.functions->ReleaseBooleanArrayElements = [] (JNIEnv*, jbooleanArray, jboolean*, jint)
129+
{
130+
};
131+
132+
auto result = jni::GetArrayElements<jni::jboolean>(env, arrayValue.Ref());
133+
jni::ReleaseArrayElements<jni::jboolean>(env, arrayValue.Ref(), std::get<0>(result).get());
134+
jni::ReleaseArrayElements<jni::jboolean>(env, arrayValue.Ref(), std::move(std::get<0>(result)));
135+
}
136+
118137
static void TestArrayRegion()
119138
{
120139
static Testable<jni::jarray<jni::jboolean>> arrayValue;
@@ -261,12 +280,9 @@ int main()
261280
NewArray
262281
jobject (*GetObjectArrayElement)(JNIEnv*, jobjectArray, jsize);
263282
void (*SetObjectArrayElement)(JNIEnv*, jobjectArray, jsize, jobject);
264-
265-
jboolean* (*GetArrayElements)(JNIEnv*, jbooleanArray, jboolean*);
266-
void (*ReleaseArrayElements)(JNIEnv*, jbooleanArray,
267-
jboolean*, jint);
268283
*/
269284

285+
TestArrayElements();
270286
TestArrayRegion();
271287

272288
TestMakeNativeMethod();

0 commit comments

Comments
 (0)