Skip to content

Commit bf245c0

Browse files
committed
Merge branch 'release/0.3.21'
2 parents 57b88f2 + 02a371d commit bf245c0

File tree

7 files changed

+64
-17
lines changed

7 files changed

+64
-17
lines changed

dart_native/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.3.21
2+
3+
* [Fix] Some crash on Android.
4+
15
## 0.3.20
26

37
* [Feature] Support 64 bit on Android.

dart_native/android/src/main/jni/dart_native.cpp

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,20 @@
99
#include "dn_method_call.h"
1010
#include "dn_signature_helper.h"
1111
#include "dn_callback.h"
12+
#include "jni_object_ref.h"
1213

1314
extern "C"
1415
{
1516

1617
static JavaVM *gJvm = nullptr;
17-
static jobject gClassLoader;
18+
static JavaGlobalRef<jobject> *gClassLoader = nullptr;
1819
static jmethodID gFindClassMethod;
1920
static pthread_key_t detachKey = 0;
2021

2122
/// for invoke result compare
22-
static jclass gStrCls;
23+
static JavaGlobalRef<jclass> *gStrCls = nullptr;
2324

24-
/// key is jobject, value is pai which contain jclass and reference count
25+
/// key is jobject, value is pair which contain jclass and reference count
2526
static std::map<jobject, std::pair<jclass, int> > objectGlobalReference;
2627

2728
/// protect objectGlobalReference
@@ -92,13 +93,13 @@ extern "C"
9293
auto getClassLoaderMethod = env->GetMethodID(pluginClass, "getClassLoader",
9394
"()Ljava/lang/ClassLoader;");
9495
auto classLoader = env->CallObjectMethod(plugin, getClassLoaderMethod);
95-
gClassLoader = env->NewGlobalRef(classLoader);
96+
gClassLoader = new JavaGlobalRef<jobject>(env->NewGlobalRef(classLoader), env);
9697
gFindClassMethod = env->GetMethodID(classLoaderClass, "findClass",
9798
"(Ljava/lang/String;)Ljava/lang/Class;");
9899

99100
/// cache string class
100101
jclass strCls = env->FindClass("java/lang/String");
101-
gStrCls = static_cast<jclass>(env->NewGlobalRef(strCls));
102+
gStrCls = new JavaGlobalRef<jclass>(static_cast<jclass>(env->NewGlobalRef(strCls)), env);
102103

103104
env->DeleteLocalRef(classLoader);
104105
env->DeleteLocalRef(plugin);
@@ -119,7 +120,7 @@ extern "C"
119120
{
120121
env->ExceptionClear();
121122
DNDebug("findClass exception");
122-
return static_cast<jclass>(env->CallObjectMethod(gClassLoader,
123+
return static_cast<jclass>(env->CallObjectMethod(gClassLoader->Object(),
123124
gFindClassMethod,
124125
env->NewStringUTF(name)));
125126
}
@@ -227,7 +228,7 @@ extern "C"
227228
jobject obj = env->CallObjectMethodA(object, method, argValues);
228229
if (obj != nullptr)
229230
{
230-
if (env->IsInstanceOf(obj, gStrCls))
231+
if (env->IsInstanceOf(obj, gStrCls->Object()))
231232
{
232233
/// mark the last pointer as string
233234
/// dart will check this pointer
@@ -380,7 +381,8 @@ extern "C"
380381
return nullptr;
381382
}
382383

383-
char *funName = (char *)env->GetStringUTFChars(functionName, 0);
384+
char *funName = functionName == nullptr ? nullptr
385+
: (char *)env->GetStringUTFChars(functionName, 0);
384386
char **dataTypes = new char *[argumentCount + 1];
385387
void **arguments = new void *[argumentCount + 1];
386388

@@ -456,8 +458,14 @@ extern "C"
456458
sem_destroy(&sem);
457459
}
458460

459-
env->ReleaseStringUTFChars(returnTypeStr, returnType);
460-
env->ReleaseStringUTFChars(functionName, funName);
461+
if (returnTypeStr != nullptr)
462+
{
463+
env->ReleaseStringUTFChars(returnTypeStr, returnType);
464+
}
465+
if (functionName != nullptr)
466+
{
467+
env->ReleaseStringUTFChars(functionName, funName);
468+
}
461469
delete[] arguments;
462470
delete[] dataTypes;
463471

dart_native/android/src/main/jni/dn_method_call.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,3 @@ void *callNativeStringMethod(JNIEnv *env, jobject object, jmethodID methodId, jv
7373
auto javaString = (jstring)env->CallObjectMethodA(object, methodId, arguments);
7474
return convertToDartUtf16(env, javaString);
7575
}
76-
77-
void *callNativeObjectMethod(JNIEnv *env, jobject object, jmethodID methodId, jvalue *arguments)
78-
{
79-
}

dart_native/android/src/main/jni/dn_method_call.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ extern "C"
2727
void *callNativeVoidMethod(JNIEnv *env, jobject object, jmethodID methodId, jvalue *arguments);
2828

2929
void *callNativeStringMethod(JNIEnv *env, jobject object, jmethodID methodId, jvalue *arguments);
30-
void *callNativeObjectMethod(JNIEnv *env, jobject object, jmethodID methodId, jvalue *arguments);
3130

3231
const std::map<char, std::function<CallNativeMethod> > methodCallerMap = {
3332
{'C', callNativeCharMethod},
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//
2+
// Created by Hui on 6/9/21.
3+
//
4+
5+
#ifndef DART_NATIVE_JNI_OBJECT_REF_H
6+
#define DART_NATIVE_JNI_OBJECT_REF_H
7+
8+
#include <jni.h>
9+
10+
/**
11+
* Global reference will auto delete in destructor
12+
*/
13+
template <typename T>
14+
class JavaGlobalRef {
15+
public:
16+
explicit JavaGlobalRef(T t, JNIEnv *env): env(env) {
17+
this->obj = env->NewGlobalRef(t);
18+
}
19+
20+
JavaGlobalRef() = delete;
21+
22+
T Object() const { return static_cast<T>(obj); }
23+
24+
~JavaGlobalRef() { this->DeleteGlobalRef(); }
25+
26+
protected:
27+
jobject obj = nullptr;
28+
29+
private:
30+
JNIEnv *env;
31+
32+
void DeleteGlobalRef() {
33+
if (this->obj) {
34+
env->DeleteGlobalRef(this->obj);
35+
this->obj = nullptr;
36+
}
37+
}
38+
};
39+
40+
#endif //DART_NATIVE_JNI_OBJECT_REF_H

dart_native/example/pubspec.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ packages:
168168
path: ".."
169169
relative: true
170170
source: path
171-
version: "0.3.20"
171+
version: "0.3.21"
172172
dart_native_gen:
173173
dependency: transitive
174174
description:

dart_native/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: dart_native
22
description: Write native code using Dart. This package liberates you from native code and low performance channel.
3-
version: 0.3.20
3+
version: 0.3.21
44
homepage: https://github.com/dart-native/dart_native
55

66
environment:

0 commit comments

Comments
 (0)