2
0

JNI reference memory management.

This commit is contained in:
Maarten Billemont 2019-09-24 21:26:07 -04:00
parent 1c6a5256c1
commit bf5e30c2c7

View File

@ -9,8 +9,13 @@
static jobject logger; static jobject logger;
static JNIEnv *env; static JNIEnv *env;
static int counter;
void mpw_log_app(LogLevel level, const char *format, ...) { void mpw_log_app(LogLevel level, const char *format, ...) {
va_list args;
va_start( args, format );
if (logger && (*env)->PushLocalFrame( env, 16 ) == OK) {
jmethodID method = NULL; jmethodID method = NULL;
jclass class = (*env)->GetObjectClass( env, logger ); jclass class = (*env)->GetObjectClass( env, logger );
if (level >= LogLevelTrace) if (level >= LogLevelTrace)
@ -24,18 +29,17 @@ void mpw_log_app(LogLevel level, const char *format, ...) {
else if (level <= LogLevelError) else if (level <= LogLevelError)
method = (*env)->GetMethodID( env, class, "error", "(Ljava/lang/String;)V" ); method = (*env)->GetMethodID( env, class, "error", "(Ljava/lang/String;)V" );
va_list args;
va_start( args, format );
if (class && method && logger) {
char *message = NULL; char *message = NULL;
int length = vasprintf(&message, format, args); int length = vasprintf( &message, format, args );
if (length > 0) if (message) {
(*env)->CallVoidMethod(env, logger, method, (*env)->NewStringUTF( env, message )); (*env)->CallVoidMethod( env, logger, method, (*env)->NewStringUTF( env, message ) );
if (message) mpw_free( &message, (size_t)max( 0, length ) );
mpw_free(&message, (size_t) length);
} }
else
(*env)->PopLocalFrame( env, NULL );
return;
}
// Can't log via slf4j, fall back to cli logger. // Can't log via slf4j, fall back to cli logger.
mpw_vlog_cli( level, format, args ); mpw_vlog_cli( level, format, args );
@ -50,7 +54,7 @@ JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void* reserved) {
jmethodID method = (*env)->GetStaticMethodID( env, class, "getLogger", "(Ljava/lang/String;)Lorg/slf4j/Logger;" ); jmethodID method = (*env)->GetStaticMethodID( env, class, "getLogger", "(Ljava/lang/String;)Lorg/slf4j/Logger;" );
jstring name = (*env)->NewStringUTF( env, "com.lyndir.masterpassword.algorithm" ); jstring name = (*env)->NewStringUTF( env, "com.lyndir.masterpassword.algorithm" );
if (class && method && name) if (class && method && name)
logger = (*env)->CallStaticObjectMethod( env, class, method, name ); logger = (*env)->NewGlobalRef( env, (*env)->CallStaticObjectMethod( env, class, method, name ) );
else else
wrn( "Couldn't initialize JNI logger." ); wrn( "Couldn't initialize JNI logger." );