简体   繁体   中英

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

JNI file looks like,

#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

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.

JNI DETECTED ERROR IN APPLICATION: use of deleted weak global reference 0xffffffff from void com.opencv_test_FaceRecognizer.predict(long, int[], double[])

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM