user17139865
user17139865

Reputation: 23

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?

Upvotes: 2

Views: 445

Answers (1)

Botje
Botje

Reputation: 30817

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.

Upvotes: 2

Related Questions