@@ -24,20 +24,25 @@ extern "C"
24
24
// / key is jobject, value is pai which contain jclass and reference count
25
25
static std::map<jobject, std::pair<jclass, int > > objectGlobalReference;
26
26
27
+ // / protect objectGlobalReference
28
+ std::mutex globalReferenceMtx;
29
+
27
30
void _addGlobalObject (jobject globalObject, jclass globalClass)
28
31
{
32
+ globalReferenceMtx.lock ();
33
+ std::lock_guard<std::mutex> lk (globalReferenceMtx);
29
34
std::pair<jclass, int > objPair = std::make_pair (globalClass, 0 );
30
35
objectGlobalReference[globalObject] = objPair;
36
+ globalReferenceMtx.unlock ();
31
37
}
32
38
33
39
jclass _getGlobalClass (jobject globalObject)
34
40
{
35
- if (objectGlobalReference.find (globalObject) != objectGlobalReference.end ())
36
- {
37
- std::pair<jclass, int > objPair = objectGlobalReference[globalObject];
38
- return objPair.first ;
39
- }
40
- return nullptr ;
41
+ globalReferenceMtx.lock ();
42
+ auto it = objectGlobalReference.find (globalObject);
43
+ auto cls = it != objectGlobalReference.end () ? it->second .first : nullptr ;
44
+ globalReferenceMtx.unlock ();
45
+ return cls;
41
46
}
42
47
43
48
void _detachThreadDestructor (void *arg)
@@ -195,7 +200,8 @@ extern "C"
195
200
jclass cls = _getGlobalClass (object);
196
201
if (cls == nullptr )
197
202
{
198
- DNError (" invokeNativeMethod not find class" );
203
+ // / maybe use cache pointer but jobject is release
204
+ DNError (" invokeNativeMethod not find class, check pointer and jobject lifecycle is same" );
199
205
return nullptr ;
200
206
}
201
207
@@ -209,7 +215,7 @@ extern "C"
209
215
}
210
216
211
217
char *methodSignature = generateSignature (dataTypes, argumentCount, returnType);
212
- DNDebug (" call method %s %s" , methodName, methodSignature);
218
+ // DNDebug("call method %s %s", methodName, methodSignature);
213
219
jmethodID method = env->GetMethodID (cls, methodName, methodSignature);
214
220
215
221
auto it = methodCallerMap.find (*returnType);
@@ -285,11 +291,13 @@ extern "C"
285
291
// / reference counter
286
292
void _updateObjectReference (jobject globalObject, bool isRetain)
287
293
{
294
+ globalReferenceMtx.lock ();
288
295
DNDebug (" _updateObjectReference %s" , isRetain ? " retain" : " release" );
289
296
auto it = objectGlobalReference.find (globalObject);
290
297
if (it == objectGlobalReference.end ())
291
298
{
292
299
DNError (" _updateObjectReference %s error not contain this object!!!" , isRetain ? " retain" : " release" );
300
+ globalReferenceMtx.unlock ();
293
301
return ;
294
302
}
295
303
@@ -298,6 +306,7 @@ extern "C"
298
306
// / dart object retain this dart object
299
307
// / reference++
300
308
it->second .second += 1 ;
309
+ globalReferenceMtx.unlock ();
301
310
return ;
302
311
}
303
312
@@ -311,8 +320,8 @@ extern "C"
311
320
env->DeleteGlobalRef (it->second .first );
312
321
objectGlobalReference.erase (it);
313
322
env->DeleteGlobalRef (globalObject);
314
- return ;
315
323
}
324
+ globalReferenceMtx.unlock ();
316
325
}
317
326
318
327
// / release native object from cache
@@ -387,7 +396,7 @@ extern "C"
387
396
auto argTypeString = (jstring)env->GetObjectArrayElement (argumentTypes, i);
388
397
auto argument = env->GetObjectArrayElement (argumentsArray, i);
389
398
dataTypes[i] = (char *)env->GetStringUTFChars (argTypeString, 0 );
390
-
399
+ DNError ( " not register this dart port! %s " , dataTypes[i]);
391
400
if (strcmp (dataTypes[i], " java.lang.String" ) == 0 )
392
401
{
393
402
arguments[i] = (jstring)argument == nullptr ? reinterpret_cast <uint16_t *>((char *)" " )
@@ -401,6 +410,7 @@ extern "C"
401
410
arguments[i] = gObj ;
402
411
403
412
env->DeleteLocalRef (objCls);
413
+ DNError (" over %s" , dataTypes[i]);
404
414
}
405
415
406
416
env->DeleteLocalRef (argTypeString);
0 commit comments