• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Android NDK + pthread - applciation crash

Leshik

Lurker
Hello everyone.
I am never programming to Android, this is my first application.
I create simpla application with two threads:
first - Java UI thread, and second thread created by calling native function from Java code.
I want call Java function for print text in TextView, but when I call env->FindClass("test/app/Test"); application did crashed http://img706.imageshack.us/img706/7047/screenshot20100113at145.png.
the AttachCurrentThread return 0.

this is example of my native code:

Code:
#include "test.h"
#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>

JavaVM* g_jvm = 0;

JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void * reserved)
{
    g_jvm = vm;
    return JNI_VERSION_1_6;
}

void JNI_OnUnload(JavaVM *vm, void *reserved)
{
    g_jvm = 0;
}

void* threadProc(void*)
{
    JNIEnv* env = 0;
    int res = g_jvm->AttachCurrentThread(&env, NULL); // res == 0
    
    jclass jclsTest = env->FindClass("test/app/Test"); // if comment this line, application not crashed
    
    g_jvm->DetachCurrentThread();
    env = 0;
    
    return 0;
}

JNIEXPORT jboolean JNICALL Java_test_app_Test_initialize(JNIEnv* env, jobject obj)
{
    jclass jcls = env->FindClass("test/app/Test");
    jmethodID mid = env->GetMethodID(jcls, "messageOutput", "(Ljava/lang/String;)V");
    
    env->CallVoidMethod(obj, mid, env->NewStringUTF("start thread"));
    
    pthread_t thread = 0;
    pthread_create(&thread, NULL, threadProc, NULL);
    
    env->CallVoidMethod(obj, mid, env->NewStringUTF("thread started"));
    
    return true;
}
 
first, 0 != NULL :) Don't assign scalars to pointers...

In threadProc(), your throwing away the result of the AttachCurrentThread(). How do you know env has be set properly?

I would guess that deferencing the FindClass() method via the env object is failing because env may be NULL.
 
Thanks for your reply, but code with some changes also not work

If comment line
jclass jclsTest = env->FindClass("test/app/Test");
application not crashed.

Code:
#include "test.h"
#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>

JavaVM* g_jvm = NULL;

JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void * reserved)
{
    g_jvm = vm;
    return JNI_VERSION_1_6;
}

void JNI_OnUnload(JavaVM *vm, void *reserved)
{
    g_jvm = NULL;
}

void* threadProc(void*)
{
    sleep(10);
    if(g_jvm == NULL)
    {
        return NULL;
    }
    
    JNIEnv* env = NULL;
    g_jvm->AttachCurrentThread(&env, NULL);
    
    if(env == NULL)
    {
        return NULL;
    }
    
    jclass jclsTest = env->FindClass("test/app/Test");
    
    g_jvm->DetachCurrentThread();
    
    return NULL;
}

JNIEXPORT jboolean JNICALL Java_test_app_Test_initialize(JNIEnv* env, jobject obj)
{
    if(g_jvm == NULL)
    {
        return JNI_FALSE;
    }
    
    g_jvm->AttachCurrentThread(&env, NULL);
    
    if(env == NULL)
    {
        return JNI_FALSE;
    }
    
    jclass jcls = env->FindClass("test/app/Test");
    jmethodID mid = env->GetMethodID(jcls, "messageOutput", "(Ljava/lang/String;)V");
    
    env->CallVoidMethod(obj, mid, env->NewStringUTF("thread start"));
    
    pthread_t thread = 0;
    pthread_create(&thread, NULL, threadProc, NULL);
    pthread_join(thread, NULL);
    
    env->CallVoidMethod(obj, mid, env->NewStringUTF("thread finish"));
    
    
    return JNI_TRUE;
}
 
if I change
jclass jclsTest = env->FindClass("test/app/Test"); in void* threadProc(void*) function by
jclass jclsview = env->FindClass("android/widget/TextView");
programm not crashed,
may be I am not maked some actions with Test class ?

this is implementation of Test class:

Code:
package test.app;

import android.app.Activity;
import android.os.Bundle;
import android.text.format.Time;
import android.widget.TextView;

public class Test extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        initialize();
    }
...
...
 
What do you mean 'Crash'? Can you support the log information. I think the problem here should be happened in android.mk. Can you post your android.mk?
 
Back
Top Bottom