JNI检测到应用程序中的错误:使用已删除的弱全局参考
JNI DETECTED ERROR IN APPLICATION: use of deleted weak global reference
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
看起来像
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);
}
但是当我尝试预测时,应用程序会随着以下logcat错误崩溃。
JNI检测到的应用中的错误:使用已删除的弱全局 参考0xffffffff来自void com.opencv_test_facerecognizer.predict(long,int [], double [])
这发生了,因为本机方法路径为
Java_com_opencv_test_FaceRecognizer_predict
-> com.opencv.test.FaceRecognizer#predict
,但此类路径是com.idesign.opencvmaketest.FaceRecognizer
相关文章:
- 将数组的地址分配给变量并删除
- 删除一个线程上有数百万个字符串的大型哈希映射会影响另一个线程的性能
- C/C++编译器通常会删除重复的库吗
- 从链接列表c++中删除一个项目
- C++如何通过用户输入删除列表元素
- 为什么在C++中使用私有复制构造函数与删除复制构造函数
- 是否需要删除包含对象的"pair"?
- 如何在自删除后将对象设置为nullptr
- 迭代时从向量和内存中删除对象
- 为什么合成的复制分配运算符被定义为如果类有参考成员,则将其定义为删除
- C 将对象删除为参考
- 为什么此C 代码不起作用 - 试图删除集合中的元素时,请参考错误
- 通过参考const成员通过参考时,尝试引用已删除的函数
- 对可删除对象的非持有参考
- 为什么删除字符串迭代器会产生const char参考
- 为什么参考可以保存指针指向指针的内容,即使指针已删除
- JNI检测到应用程序中的错误:使用已删除的弱全局参考
- 我已经创建了一个精灵的否,并以相同的参考将它们添加到场景中,现在,如何使用标签删除它们
- 如何删除自己与参考
- 为什么我不能删除我的节点和参考