1. Are you ready for the Galaxy S20? Here is everything we know so far!

Android NDK + pthread - applciation crash

Discussion in 'Android Development' started by Leshik, Jan 13, 2010.

  1. Leshik

    Leshik Lurker
    Thread Starter

    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 (Text):
    1.  
    2. #include "test.h"
    3. #include <pthread.h>
    4. #include <stdlib.h>
    5. #include <stdio.h>
    6.  
    7. JavaVM* g_jvm = 0;
    8.  
    9. JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void * reserved)
    10. {
    11.     g_jvm = vm;
    12.     return JNI_VERSION_1_6;
    13. }
    14.  
    15. void JNI_OnUnload(JavaVM *vm, void *reserved)
    16. {
    17.     g_jvm = 0;
    18. }
    19.  
    20. void* threadProc(void*)
    21. {
    22.     JNIEnv* env = 0;
    23.     int res = g_jvm->AttachCurrentThread(&env, NULL); // res == 0
    24.    
    25.     jclass jclsTest = env->FindClass("test/app/Test"); // if comment this line, application not crashed
    26.    
    27.     g_jvm->DetachCurrentThread();
    28.     env = 0;
    29.    
    30.     return 0;
    31. }
    32.  
    33. JNIEXPORT jboolean JNICALL Java_test_app_Test_initialize(JNIEnv* env, jobject obj)
    34. {
    35.     jclass jcls = env->FindClass("test/app/Test");
    36.     jmethodID mid = env->GetMethodID(jcls, "messageOutput", "(Ljava/lang/String;)V");
    37.    
    38.     env->CallVoidMethod(obj, mid, env->NewStringUTF("start thread"));
    39.    
    40.     pthread_t thread = 0;
    41.     pthread_create(&thread, NULL, threadProc, NULL);
    42.    
    43.     env->CallVoidMethod(obj, mid, env->NewStringUTF("thread started"));
    44.    
    45.     return true;
    46. }
    47.  
     


  2. KeithG

    KeithG Well-Known Member

    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.
     
  3. Leshik

    Leshik Lurker
    Thread Starter

    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 (Text):
    1.  
    2. #include "test.h"
    3. #include <pthread.h>
    4. #include <stdlib.h>
    5. #include <stdio.h>
    6. #include <unistd.h>
    7.  
    8. JavaVM* g_jvm = NULL;
    9.  
    10. JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void * reserved)
    11. {
    12.     g_jvm = vm;
    13.     return JNI_VERSION_1_6;
    14. }
    15.  
    16. void JNI_OnUnload(JavaVM *vm, void *reserved)
    17. {
    18.     g_jvm = NULL;
    19. }
    20.  
    21. void* threadProc(void*)
    22. {
    23.     sleep(10);
    24.     if(g_jvm == NULL)
    25.     {
    26.         return NULL;
    27.     }
    28.    
    29.     JNIEnv* env = NULL;
    30.     g_jvm->AttachCurrentThread(&env, NULL);
    31.    
    32.     if(env == NULL)
    33.     {
    34.         return NULL;
    35.     }
    36.    
    37.     jclass jclsTest = env->FindClass("test/app/Test");
    38.    
    39.     g_jvm->DetachCurrentThread();
    40.    
    41.     return NULL;
    42. }
    43.  
    44. JNIEXPORT jboolean JNICALL Java_test_app_Test_initialize(JNIEnv* env, jobject obj)
    45. {
    46.     if(g_jvm == NULL)
    47.     {
    48.         return JNI_FALSE;
    49.     }
    50.    
    51.     g_jvm->AttachCurrentThread(&env, NULL);
    52.    
    53.     if(env == NULL)
    54.     {
    55.         return JNI_FALSE;
    56.     }
    57.    
    58.     jclass jcls = env->FindClass("test/app/Test");
    59.     jmethodID mid = env->GetMethodID(jcls, "messageOutput", "(Ljava/lang/String;)V");
    60.    
    61.     env->CallVoidMethod(obj, mid, env->NewStringUTF("thread start"));
    62.    
    63.     pthread_t thread = 0;
    64.     pthread_create(&thread, NULL, threadProc, NULL);
    65.     pthread_join(thread, NULL);
    66.    
    67.     env->CallVoidMethod(obj, mid, env->NewStringUTF("thread finish"));
    68.    
    69.    
    70.     return JNI_TRUE;
    71. }
    72.  
     
  4. Leshik

    Leshik Lurker
    Thread Starter

    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 (Text):
    1.  
    2. package test.app;
    3.  
    4. import android.app.Activity;
    5. import android.os.Bundle;
    6. import android.text.format.Time;
    7. import android.widget.TextView;
    8.  
    9. public class Test extends Activity {
    10.     /** Called when the activity is first created. */
    11.     @Override
    12.     public void onCreate(Bundle savedInstanceState) {
    13.         super.onCreate(savedInstanceState);
    14.         setContentView(R.layout.main);
    15.        
    16.         initialize();
    17.     }
    18. ...
    19. ...
    20.  
    21.  
     
  5. leewind

    leewind Lurker

    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?
     
Loading...

Share This Page

Loading...