Android JNI C ++分段错误

时间:2014-07-09 12:01:08

标签: android c++ opencv

我编写了以下本机函数来提取二进制Mat的白色像素的坐标:

JNIEXPORT void JNICALL Java_com_example_testapp_MainActivity_findCornersJNI(JNIEnv* env, jobject obj, cv::Mat In, cv::Mat Out){
    int Number_Of_Elements;
    Mat Binary_Image;
    cvtColor(In, Binary_Image, CV_BGR2GRAY);
    Mat NonZero_Locations = cv::Mat::zeros(Binary_Image.size(),Binary_Image.channels());
    if (Binary_Image.empty())
        std::cout << "Input is empty!" << std::endl;
        return;

    findNonZero(Binary_Image, NonZero_Locations);
    //cout << "Non-Zero Locations = " << NonZero_Locations << endl << endl;

    Number_Of_Elements = NonZero_Locations.total();
    //cout << "Total Number Of Array Elements = " << Number_Of_Elements << endl << endl;

    NonZero_Locations.copyTo(Out);
    delete &NonZero_Locations;
    return;
}

我在运行时遇到分段错误错误:

libc Fatal signal 7 (SIGBUS) at 0x00000000 (code=128), thread 7914

这可能是什么原因?

1 个答案:

答案 0 :(得分:1)

没有更多信息,这只是一个疯狂的猜测,但是: 根据{{​​3}},您使用Mat::zeros在堆栈上创建Mat对象:

Mat NonZero_Locations = cv::Mat::zeros(Binary_Image.size(),Binary_Image.channels());

稍后,删除本地堆栈对象。

delete &NonZero_Locations;

通常,在C ++中,您只删除动态分配的对象。离开其范围时,堆栈对象将被销毁。因此,在您的情况下,您不必(并且您不允许)删除NonZero_Locations