2017-08-14 37 views
0

我正在編寫一個Android應用程序,用於處理來自本地C(NDK r15b)的圖片(RAW格式)。即時得到以下錯誤:Android(ART)崩潰,出現錯誤JNI在應用程序中檢測到錯誤:jstring是無效的本地參考

08-14 18:08:25.407 6107-6107/compresor.app.tfg.compresor E/art: 0xa3f20b80 SpaceTypeMallocSpace begin=0x12c00000,end=0x12e17000,limit=0x2ac00000,size=2MB,capacity=384MB,non_growth_limit_capacity=384MB,name="main rosalloc space"] 
 
08-14 18:08:25.407 6107-6107/compresor.app.tfg.compresor E/art: 0xa3f1fa80 allocspace main rosalloc space live-bitmap 3[begin=0x12c00000,end=0x2ac00000] 
 
08-14 18:08:25.407 6107-6107/compresor.app.tfg.compresor E/art: 0xa3f1fac0 allocspace main rosalloc space mark-bitmap 3[begin=0x12c00000,end=0x2ac00000] 
 
08-14 18:08:25.407 6107-6107/compresor.app.tfg.compresor E/art: 0xaf99b780 SpaceTypeImageSpace begin=0x6fbf5000,end=0x6fcf9288,size=1040KB,name="/data/dalvik-cache/x86/[email protected]@boot.art"] 
 
08-14 18:08:25.407 6107-6107/compresor.app.tfg.compresor E/art: 0xaf92f1e0 imagespace /data/dalvik-cache/x86/[email protected]@boot.art live-bitmap 0[begin=0x6fbf5000,end=0x6fcf9300] 
 
08-14 18:08:25.407 6107-6107/compresor.app.tfg.compresor E/art: 0xaf92f1e0 imagespace /data/dalvik-cache/x86/[email protected]@boot.art live-bitmap 0[begin=0x6fbf5000,end=0x6fcf9300] 
 
08-14 18:08:25.407 6107-6107/compresor.app.tfg.compresor E/art: 0xaf99b900 SpaceTypeImageSpace begin=0x6fdf7000,end=0x6fe5f5a8,size=417KB,name="/data/dalvik-cache/x86/[email protected]@boot-core-libart.art"] 
 
08-14 18:08:25.407 6107-6107/compresor.app.tfg.compresor E/art: 0xaf92f400 imagespace /data/dalvik-cache/x86/[email protected]@boot-core-libart.art live-bitmap 1[begin=0x6fdf7000,end=0x6fe5f600] 
 
08-14 18:08:25.407 6107-6107/compresor.app.tfg.compresor E/art: 0xaf92f400 imagespace /data/dalvik-cache/x86/[email protected]@boot-core-libart.art live-bitmap 1[begin=0x6fdf7000,end=0x6fe5f600] 
 
08-14 18:08:25.407 6107-6107/compresor.app.tfg.compresor E/art: 0xaf99b940 SpaceTypeImageSpace begin=0x6ff12000,end=0x6ff258f8,size=78KB,name="/data/dalvik-cache/x86/[email protected]@boot-conscrypt.art"] 
 
08-14 18:08:25.407 6107-6107/compresor.app.tfg.compresor E/art: 0xaf92f500 imagespace /data/dalvik-cache/x86/[email protected]@boot-conscrypt.art live-bitmap 2[begin=0x6ff12000,end=0x6ff25900] 
 
08-14 18:08:25.407 6107-6107/compresor.app.tfg.compresor E/art: 0xaf92f500 imagespace /data/dalvik-cache/x86/[email protected]@boot-conscrypt.art live-bitmap 2[begin=0x6ff12000,end=0x6ff25900] 
 
08-14 18:08:25.407 6107-6107/compresor.app.tfg.compresor E/art: 0xaf99ba80 SpaceTypeImageSpace begin=0x6ff3c000,end=0x6ff4b040,size=60KB,name="/data/dalvik-cache/x86/[email protected]@boot-okhttp.art"] 
 
08-14 18:08:25.407 6107-6107/compresor.app.tfg.compresor E/art: 0xaf92f640 imagespace /data/dalvik-cache/x86/[email protected]@boot-okhttp.art live-bitmap 3[begin=0x6ff3c000,end=0x6ff4b100] 
 
08-14 18:08:25.407 6107-6107/compresor.app.tfg.compresor E/art: 0xaf92f640 imagespace /data/dalvik-cache/x86/[email protected]@boot-okhttp.art live-bitmap 3[begin=0x6ff3c000,end=0x6ff4b100] 
 
08-14 18:08:25.407 6107-6107/compresor.app.tfg.compresor E/art: 0xaf99bbc0 SpaceTypeImageSpace begin=0x6ff66000,end=0x6ff66260,size=608B,name="/data/dalvik-cache/x86/[email protected]@boot-core-junit.art"] 
 
08-14 18:08:25.407 6107-6107/compresor.app.tfg.compresor E/art: 0xaf92f780 imagespace /data/dalvik-cache/x86/[email protected]@boot-core-junit.art live-bitmap 4[begin=0x6ff66000,end=0x6ff66300] 
 
08-14 18:08:25.407 6107-6107/compresor.app.tfg.compresor E/art: 0xaf92f780 imagespace /data/dalvik-cache/x86/[email protected]@boot-core-junit.art live-bitmap 4[begin=0x6ff66000,end=0x6ff66300] 
 
08-14 18:08:25.407 6107-6107/compresor.app.tfg.compresor E/art: 0xaf99bd00 SpaceTypeImageSpace begin=0x6ff68000,end=0x6ff81ad0,size=102KB,name="/data/dalvik-cache/x86/[email protected]@boot-bouncycastle.art"] 
 
08-14 18:08:25.407 6107-6107/compresor.app.tfg.compresor E/art: 0xaf92fcc0 imagespace /data/dalvik-cache/x86/[email protected]@boot-bouncycastle.art live-bitmap 5[begin=0x6ff68000,end=0x6ff81b00] 
 
08-14 18:08:25.407 6107-6107/compresor.app.tfg.compresor E/art: 0xaf92fcc0 imagespace /data/dalvik-cache/x86/[email protected]@boot-bouncycastle.art live-bitmap 5[begin=0x6ff68000,end=0x6ff81b00] 
 
08-14 18:08:25.407 6107-6107/compresor.app.tfg.compresor E/art: 0xaf99be40 SpaceTypeImageSpace begin=0x6ffa0000,end=0x6ffb7f58,size=95KB,name="/data/dalvik-cache/x86/[email protected]@boot-ext.art"] 
 
08-14 18:08:25.407 6107-6107/compresor.app.tfg.compresor E/art: 0xaf92fdc0 imagespace /data/dalvik-cache/x86/[email protected]@boot-ext.art live-bitmap 6[begin=0x6ffa0000,end=0x6ffb8000] 
 
08-14 18:08:25.407 6107-6107/compresor.app.tfg.compresor E/art: 0xaf92fdc0 imagespace /data/dalvik-cache/x86/[email protected]@boot-ext.art live-bitmap 6[begin=0x6ffa0000,end=0x6ffb8000] 
 
08-14 18:08:25.407 6107-6107/compresor.app.tfg.compresor E/art: 0xaf99bf80 SpaceTypeImageSpace begin=0x6ffd7000,end=0x702246e0,size=2MB,name="/data/dalvik-cache/x86/[email protected]@boot-framework.art"] 
 
08-14 18:08:25.407 6107-6107/compresor.app.tfg.compresor E/art: 0xaf92ff20 imagespace /data/dalvik-cache/x86/[email protected]@boot-framework.art live-bitmap 7[begin=0x6ffd7000,end=0x70224700] 
 
08-14 18:08:25.407 6107-6107/compresor.app.tfg.compresor E/art: 0xaf92ff20 imagespace /data/dalvik-cache/x86/[email protected]@boot-framework.art live-bitmap 7[begin=0x6ffd7000,end=0x70224700] 
 
08-14 18:08:25.407 6107-6107/compresor.app.tfg.compresor E/art: 0xaf99b7c0 SpaceTypeImageSpace begin=0x7058e000,end=0x705dbed8,size=311KB,name="/data/dalvik-cache/x86/[email protected]@boot-telephony-common.art"] 
 
08-14 18:08:25.407 6107-6107/compresor.app.tfg.compresor E/art: 0xaf9fd840 imagespace /data/dalvik-cache/x86/[email protected]@boot-telephony-common.art live-bitmap 8[begin=0x7058e000,end=0x705dbf00] 
 
08-14 18:08:25.407 6107-6107/compresor.app.tfg.compresor E/art: 0xaf9fd840 imagespace /data/dalvik-cache/x86/[email protected]@boot-telephony-common.art live-bitmap 8[begin=0x7058e000,end=0x705dbf00] 
 
08-14 18:08:25.407 6107-6107/compresor.app.tfg.compresor E/art: 0xaf92d340 SpaceTypeImageSpace begin=0x7060c000,end=0x7060d520,size=5KB,name="/data/dalvik-cache/x86/[email protected]@boot-voip-common.art"] 
 
08-14 18:08:25.407 6107-6107/compresor.app.tfg.compresor E/art: 0xaf92f180 imagespace /data/dalvik-cache/x86/[email protected]@boot-voip-common.art live-bitmap 9[begin=0x7060c000,end=0x7060d600] 
 
08-14 18:08:25.407 6107-6107/compresor.app.tfg.compresor E/art: 0xaf92f180 imagespace /data/dalvik-cache/x86/[email protected]@boot-voip-common.art live-bitmap 9[begin=0x7060c000,end=0x7060d600] 
 
08-14 18:08:25.407 6107-6107/compresor.app.tfg.compresor E/art: 0xaf92d380 SpaceTypeImageSpace begin=0x70612000,end=0x70618850,size=26KB,name="/data/dalvik-cache/x86/[email protected]@boot-ims-common.art"] 
 
08-14 18:08:25.407 6107-6107/compresor.app.tfg.compresor E/art: 0xaf9fd9a0 imagespace /data/dalvik-cache/x86/[email protected]@boot-ims-common.art live-bitmap 10[begin=0x70612000,end=0x70618900] 
 
08-14 18:08:25.407 6107-6107/compresor.app.tfg.compresor E/art: 0xaf9fd9a0 imagespace /data/dalvik-cache/x86/[email protected]@boot-ims-common.art live-bitmap 10[begin=0x70612000,end=0x70618900] 
 
08-14 18:08:25.407 6107-6107/compresor.app.tfg.compresor E/art: 0xaf92d4c0 SpaceTypeImageSpace begin=0x7061d000,end=0x7061d118,size=280B,name="/data/dalvik-cache/x86/[email protected]@boot-apache-xml.art"] 
 
08-14 18:08:25.407 6107-6107/compresor.app.tfg.compresor E/art: 0xaf9fdaa0 imagespace /data/dalvik-cache/x86/[email protected]@boot-apache-xml.art live-bitmap 11[begin=0x7061d000,end=0x7061d200] 
 
08-14 18:08:25.407 6107-6107/compresor.app.tfg.compresor E/art: 0xaf9fdaa0 imagespace /data/dalvik-cache/x86/[email protected]@boot-apache-xml.art live-bitmap 11[begin=0x7061d000,end=0x7061d200] 
 
08-14 18:08:25.407 6107-6107/compresor.app.tfg.compresor E/art: 0xaf92d600 SpaceTypeImageSpace begin=0x70635000,end=0x70644618,size=61KB,name="/data/dalvik-cache/x86/[email protected]@boot-org.apache.http.legacy.boot.art"] 
 
08-14 18:08:25.407 6107-6107/compresor.app.tfg.compresor E/art: 0xaf9fdba0 imagespace /data/dalvik-cache/x86/[email protected]@boot-org.apache.http.legacy.boot.art live-bitmap 12[begin=0x70635000,end=0x70644700] 
 
08-14 18:08:25.407 6107-6107/compresor.app.tfg.compresor E/art: 0xaf9fdba0 imagespace /data/dalvik-cache/x86/[email protected]@boot-org.apache.http.legacy.boot.art live-bitmap 12[begin=0x70635000,end=0x70644700] 
 
08-14 18:08:25.407 6107-6107/compresor.app.tfg.compresor E/art: 0xa5f3bdc0 SpaceTypeZygoteSpace begin=0x747ca000,end=0x748d2000,size=1056KB,name="Zygote space"] 
 
08-14 18:08:25.407 6107-6107/compresor.app.tfg.compresor E/art: 0xaf9fdca0 allocspace zygote/non moving space live-bitmap 0[begin=0x747ca000,end=0x748d2000] 
 
08-14 18:08:25.407 6107-6107/compresor.app.tfg.compresor E/art: 0xaf9fdce0 allocspace zygote/non moving space mark-bitmap 0[begin=0x747ca000,end=0x748d2000] 
 
08-14 18:08:25.407 6107-6107/compresor.app.tfg.compresor E/art: 0xa3f20a80

我在Java中運行的代碼是:

package compresor.app.tfg.compresor; 

    import android.support.v7.app.AppCompatActivity; 
    import android.os.Bundle; 
    import android.view.View; 
    import android.widget.Button; 
    import android.widget.TextView; 

    public class MainActivity extends AppCompatActivity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 


    Button boton=(Button)findViewById(R.id.button); 




    boton.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      // Code here executes on main thread after user presses button 

      MyNDK myNDK=new MyNDK(); 
      char[] arguments; 
      arguments= new char[]{' '}; 

      myNDK.compresion(arguments); 


     } 
    }); 



} 
} 



package compresor.app.tfg.compresor; 



public class MyNDK { 

    static { 

    System.loadLibrary("ndkmain"); 
} 

public native void compresion(char[] argv); 

    } 

對應的本地代碼:

 #include <jni.h> 
     #include<Principal.h> 
     #include <compresor_app_tfg_compresor_MyNDK.h> 


    JNIEXPORT void JNICALL 
    Java_compresor_app_tfg_compresor_MyNDK_compresion(JNIEnv* env, jobject , 
    jobjectArray jargv){ 


    //jargv is a Java array of Java strings 
    int argc = env->GetArrayLength(jargv); 
    typedef char *pchar; 
    pchar *argv = new pchar[argc]; 
    int i; 
    for (i = 0; i < argc; i++) { 
     jstring js = (jstring) env->GetObjectArrayElement(jargv, i); //A Java string 
     const char *pjc = env->GetStringUTFChars(js,NULL); //A pointer to a Java-managed char buffer 
     size_t jslen = strlen(pjc); 
     argv[i] = new char[jslen + 1]; //Extra char for the terminating null 
     strcpy(argv[i], pjc); //Copy to *our* buffer. We could omit that, but IMHO this is cleaner. Also, const correctness. 
     env->ReleaseStringUTFChars(js, pjc); 
    } 

    //Call main 

    Principal *pa = Principal::CreateInstance(argc, argv); 
    pa->Run(); 
     pa->FreeInstance(); 

    //Now free the array 
    for (i = 0; i < argc; i++) 
     delete[] argv[i]; 
    delete[] argv; 




} 

我不瞭解錯誤的來源,但似乎有些錯誤興與寫得不好的本地代碼做的,我在不合適的內存的部分寫入

感謝您在adavance

UPDATE

我有修改與字符串代碼如下:

package compresor.app.tfg.compresor; 

    import android.support.v7.app.AppCompatActivity; 
    import android.os.Bundle; 
    import android.view.View; 
    import android.widget.Button; 
    import android.widget.TextView; 

public class MainActivity extends AppCompatActivity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 


    Button boton=(Button)findViewById(R.id.button); 


    boton.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      // Code here executes on main thread after user presses button 

      MyNDK myNDK = new MyNDK(); 
      String[] arguments = new String[]{"-C","-Qt","1"}; 

      myNDK.compresion(arguments); 



     } 
    }); 

     }} 

應用程序退出時沒有提供任何錯誤消息。在C++中的應用程序工作正常,當您給出圖像時,comprresor會生成一個文件(out.bts),例如image.bmp或image.dng。

compressor.exe -C -Qt 1 -i image.bmp -o out.bts -c config.txt -w 512 -h 
512 -b 2.0 -a 0 -Q 0.8 -Xi 0.375 -d 0.5 

我Android.mk文件看起來像:

LOCAL_PATH := $(call my-dir) 
include $(CLEAR_VARS) 
LOCAL_MODULE := ndkmain 
LOCAL_SRC_FILES :=ndkmain.cpp CodificadorAritmetico.cpp cuentasim.cpp 
Estima.cpp principal.cpp sb_utils.cpp TBitsContainer.cpp TBitStream.cpp 
TFichero.cpp TFormatTransform.cpp TImg_SLTWFloat.cpp TImgCoder.cpp 
TQMatrix.cpp TTransformada.cpp totales.cpp 
LOCAL_CPP_FEATURES += exceptions 
LOCAL_LDLIBS := -llog -landroid -lEGL -lGLESv2 -lz -lm 
LOCAL_LDFLAGS := -Wl,--allow-multiple-definition 
include $(BUILD_SHARED_LIBRARY) 

和我Application.mk樣子:

APP_MODULES:=ndkmain 
APP_ABI := all 
APP_STL:=stlport_shared 

我不是在C++上comprresor.exe參數能夠看到錯誤來自哪裏

+0

'jstring js'是一個不及時發佈的本地引用。在釋放UTF字符串後,您應該在循環中調用DeleteLocalRef(js)。 –

+0

順便說一句,strdup()會讓你的生活更輕鬆 –

+0

我已經添加了env-> DeleteLocalRef(js);在env-> ReleaseStringUTFChars(js,pjc)之後;但不工作。謝謝您的回答 – Gigasnike95

回答

1

在Java中,char []字符串不是同義詞。你的方法被定義爲compresion(char [] argv),並且你將char數組傳遞給它。所以,你的本機代碼必須處理jchar數組,而不是字符串數組:

JNIEXPORT void JNICALL 
Java_compresor_app_tfg_compresor_MyNDK_compresion(JNIEnv* env, jobject , 
jcharArray jargv) { 

    //jargv is a Java array of characters 
    int argc = env->GetArrayLength(jargv); 
    jchar *argv = new jchar[argc]; 
    env->GetCharArrayRegion(jargv, 0, argc, argv); 

注意jchar不是C 焦炭,而是uint16_t

但我想這個小片段不是你真正想要的。

很可能,您的意圖是傳遞一個Java字符串數組。這意味着您的Java應該是不同的:

MyNDK myNDK = new MyNDK(); 
String[] arguments = new String[]{"1", "2", "3"}; 

myNDK.compresion(arguments); 
… 
public native void compresion(String[] argv); 

現在, C++代碼應工作。

PS幾個改進建議:

  • 如果你聲明的本地方法靜態,你並不需要構建myNDK
  • 您可以使用strdup()而不是new char [] + strcpy
  • 您應該致電env-> DeleteLocalRef(js)如果argc可能不僅僅是幾個。
  • 您不應該在UI線程上執行大量計算(例如壓縮)。
  • 在英語中,「壓縮」和「壓縮器」拼寫與double s
+0

顯示UPDATE。非常感謝您的回答Alex – Gigasnike95

+0

對不起,我不知所措。首先,如果你想運行一個C++應用程序,你可以在沒有JNI的情況下執行它 - Java具有** Runtime.exec()**。其次,看起來你沒有通過所有參數('-i image.bmp -o out.bts -c config.txt -w 512 -h 512 -b 2.0 -a 0 -Q 0.8 -Xi 0.375 -d 0.5 ')到C++。最後,建議使用絕對文件路徑,因爲您的應用程序沒有可寫的工作目錄。哦,並確保你有寫權限到你想要放置結果的目錄。 –

+0

我的目標是創建一個實現該代碼不運行.exe的Android應用程序。我不知道如何將必要的路徑添加到文件。感謝您的回答 – Gigasnike95

相關問題