简体   繁体   中英

Question about C++ global variable in JNI on android

Is using C++ global variable in JNI on android acceptable?

If so, I'd like to know the life-cycle of it.

When a.cpp is connected to b.java and instance of b is created(is global variable initialized at this point?) and destroyed(is global variable destroyed at this point?).

In a nutshell, global variable in C++ side share it's won life-cycle with connected Java instance?

The lifetime of native objects is tied to the lifetime of the native library that hosts them. This, in turn, is governed by the lifetime of the Java ClassLoader that loaded the library :

In addition, native libraries can be unloaded when their corresponding class loaders are garbage collected.

In Android applications that will never happen, so you can assume that your native objects survive as long as the application does.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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