简体   繁体   English

JNI检测到应用程序错误:使用已删除的弱全局引用

[英]JNI DETECTED ERROR IN APPLICATION: use of deleted weak global reference

JNI file looks like, JNI文件看起来像

#include <jni.h>
#include <opencv2/core.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/features2d.hpp>
#include <opencv2/face.hpp>
#include <opencv2/core/utility.hpp>
#include <vector>
#include <opencv2/highgui.hpp>

#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>

#include <fstream>
#include <sstream>
#include <map>

#include <android/log.h>

#define LOG_TAG "Opencv3_test"
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)

using namespace std;
using namespace cv;
using namespace cv::face;


extern "C" {

JNIEXPORT void JNICALL Java_com_opencv_test_FaceRecognizer_train
        (JNIEnv *, jobject, jlongArray, jintArray);

JNIEXPORT void JNICALL Java_com_opencv_test_FaceRecognizer_predict
        (JNIEnv *, jobject, long, jintArray, jdoubleArray);

Ptr<LBPHFaceRecognizer> model;

JNIEXPORT void JNICALL Java_com_opencv_test_FaceRecognizer_train
        (JNIEnv *env, jobject thisObj, jlongArray images1, jintArray labels1) {
    model = LBPHFaceRecognizer::create();

    jsize length = (*env).GetArrayLength(images1);
    jlong *mats = (*env).GetLongArrayElements(images1, 0);
    jint *matLabels = (*env).GetIntArrayElements(labels1, 0);

    if (length > 0) {
        vector<Mat> images;
        vector<int> labels;
        for (int i = 0; i < length; i++) {
            Mat &mGr = *(Mat *) mats[i];
            images.push_back(mGr);
            labels.push_back(matLabels[i]);
            mGr.release();
        }
        model->train(images, labels);
        images.clear();
        labels.clear();
    }
    (*env).ReleaseLongArrayElements(images1, mats, 0);
    (*env).ReleaseIntArrayElements(labels1, matLabels, 0);
}

JNIEXPORT void JNICALL Java_com_opencv_test_FaceRecognizer_predict
        (JNIEnv *env, jobject thisObj, long images1, jintArray label, jdoubleArray confidence) {
    Mat &mGr = *(Mat *) images1;
    jint *labelBody = (*env).GetIntArrayElements(label, 0);
    jdouble *confidenceBody = (*env).GetDoubleArrayElements(confidence, 0);
    int &myLabel = labelBody[0];
    double &myConfidence = confidenceBody[0];
    LOGD("Prediction started in JNI");
    model->predict(mGr, myLabel, myConfidence);
    LOGD("Prediction ended in JNI");
    (*env).ReleaseIntArrayElements(label, labelBody, 0);
    (*env).ReleaseDoubleArrayElements(confidence, confidenceBody, 0);
}    
}

FaceRecognizer.java looks like FaceRecognizer.java看起来像

public class FaceRecognizer {

    private static final String TAG = FaceRecognizer.class.getSimpleName();

    public FaceRecognizer() {
    }

    public void predict(Mat mGrey, int[] label, double[] confidence) {
        predict(mGrey.getNativeObjAddr(), label, confidence);
    }

    public boolean train() {
        File root = new File(activity.getFilesDir().getAbsolutePath() + "/" + TRAIN.name() + "/");

        FilenameFilter pngFilter = new FilenameFilter() {
            public boolean accept(File dir, String name) {
                return name.toLowerCase().endsWith(".jpg");

            }
        };

        File[] imageFiles = root.listFiles(pngFilter);
        long[] mats = new long[imageFiles.length];
        int[] IDs = new int[imageFiles.length];
        int counter = 0;
        for (File file : imageFiles) {
            Log.v(TAG, file.getAbsolutePath());
            Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
            if (bitmap != null) {
                Bitmap bmp32 = bitmap.copy(Bitmap.Config.ARGB_8888, true);
                Mat b = new Mat(bmp32.getWidth(), bmp32.getHeight(), CvType.CV_8UC1);
                Utils.bitmapToMat(bmp32, b);
                Imgproc.cvtColor(b, b, Imgproc.COLOR_RGB2GRAY);
                int id = Integer.parseInt(file.getAbsolutePath().split("_")[1]);
                Log.v(TAG, "id : " + id);
                mats[counter] = b.getNativeObjAddr();
                IDs[counter] = id;
                counter++;
            }
        }
        train(mats, IDs);
        return true;
    }

    public native void train(long[] mats, int[] label);

    public native void predict(long mats, int[] integer, double[] d);
}

But when I am trying predict, app crashes with following logcat error. 但是,当我尝试预测时,应用程序崩溃并显示以下logcat错误。

JNI DETECTED ERROR IN APPLICATION: use of deleted weak global reference 0xffffffff from void com.opencv_test_FaceRecognizer.predict(long, int[], double[]) JNI检测到应用程序错误:使用来自void com.opencv_test_FaceRecognizer.predict(long,int [],double [])的已删除弱全局引用0xffffffff

This happens because native method path is 发生这种情况是因为本机方法路径是
Java_com_opencv_test_FaceRecognizer_predict
-> com.opencv.test.FaceRecognizer#predict but this class path is com.idesign.opencvmaketest.FaceRecognizer -> com.opencv.test.FaceRecognizer#predict但此类路径为com.idesign.opencvmaketest.FaceRecognizer

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 JNI在应用程序中检测到错误:使用已删除的本地引用0x1 - JNI DETECTED ERROR IN APPLICATION: use of deleted local reference 0x1 JNI错误:访问过时的弱全局引用 - JNI Error: accessed stale weak global reference JNI错误访问过时的弱全局引用 - JNI Error accessed stale weak global reference SoundPoolThread 通过 JNI 错误导致 SIGSEGV“访问已删除的全局引用” - SoundPoolThread causing SIGSEGV via JNI ERROR "accessed deleted global reference" android.view.RenderNode JNI ERROR(app bug):弱全局引用表溢出 - android.view.RenderNode JNI ERROR (app bug): weak global reference table overflow 是什么导致 JNI 错误“使用已删除的本地引用”? - What causes the JNI error "use of deleted local reference"? 如何调试:应用程序中的JNI检测到错误:使用无效的jobject - How to debug: JNI DETECTED ERROR IN APPLICATION: use of invalid jobject Android(ART)崩溃,错误JNI DETECTED ERROR IN APPLICATION:jarray是无效的堆栈间接引用表或无效的引用 - Android (ART) crash with error JNI DETECTED ERROR IN APPLICATION: jarray is an invalid stack indirect reference table or invalid reference Android(ART)崩溃,错误JNI DETECTED ERROR IN APPLICATION:jstring是无效的本地引用 - Android (ART) crash with error JNI DETECTED ERROR IN APPLICATION: jstring is an invalid local reference JNI在Brother SDK上的应用程序中检测到错误 - JNI DETECTED ERROR IN APPLICATION on Brother SDK
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM