简体   繁体   English

如何使用JNI管理C ++代码中的静态变量?

[英]How are the static variables in C++ code managed with JNI?

My question is, Assume I have a c++ class with singleton and through JNI methods I call this singleton, does each time I call from java to c++ the singleton variable changed because its static, or it remains the same ? 我的问题是,假设我有一个带有单例的c ++类,并且我通过JNI方法调用这个单例,每次调用java到c ++时,单例变量因为静态而改变,还是保持不变? and does each time I call from java to c++ it run the method on new thread or no? 并且每次我从java调用c ++它在新线程上运行方法还是没有?

Example Code: 示例代码:

in Java: 在Java中:

class NativeLib
{
  public native void updateFrame();
}

in C/C++ : 在C / C ++中:

JNIEXPORT void JNICALL Java_com_Company_NativeLib_NativeLib_updateFrame()
{
   sceneManager::getInstance()->updateFrame();
}

Does sceneManager::getInstance() at each call return new instance or the latest created instance since its a static variable. 每次调用时sceneManager :: getInstance()是否返回新实例或最新创建的实例,因为它是一个静态变量。

my main problem in android my app crash without showing any kind of logCat information why it crashed. 我在android我的应用程序崩溃的主要问题,没有显示任何类型的logCat信息为什么它崩溃。

but if I comment the sceneManager::getInstance()->updateFrame(); 但如果我评论了sceneManager :: getInstance() - > updateFrame(); , it never crash so what I think is when ever Java make a call to C++ it is in a new thread which mean static variables does not 它永远不会崩溃,所以我认为当Java调用C ++时,它是在一个新线程中,这意味着静态变量不会

Think of the VM as a library of C/C++ code that your application has called into. 可以将VM视为应用程序调用的C / C ++代码库。 Sometimes it calls back into your code. 有时它会回调你的代码。

Singletons will not be recreated -- it's just a method call. 单身人士不会被重新创建 - 这只是一个方法调用。 The Dalvik VM threads are just pthreads, and whichever thread executes the native call from Java-language code will be the thread that executes your C++ code. Dalvik VM线程只是pthread,无论哪个线程执行来自Java语言代码的本机调用,都将是执行C ++代码的线程。

Your best bet is to attach a native debugger. 您最好的选择是附加本机调试器。 FWIW, one way to crash with nothing in logcat is to have native recursion that overflows the stack. FWIW,在logcat中没有任何东西崩溃的一种方法是使本地递归溢出堆栈。 Another way is to change the signal handlers for SIGSEGV / SIGBUS and friends. 另一种方法是更改​​SIGSEGV / SIGBUS和朋友的信号处理程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM