alvaro.delaserna
alvaro.delaserna

Reputation: 549

Android JNI C++ segmentation fault

I wrote the following native function for extracting the coordinates of the white pixels of a binary 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;
}

I am getting a segmentation fault error at runtime:

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

What can be the cause of this?

Upvotes: 0

Views: 256

Answers (1)

MatthiasB
MatthiasB

Reputation: 1759

Without more information this is only a wild guess, but: According to the OpenCV documentation, you use Mat::zeros to create a Mat object on the stack:

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

Later, you delete the local stack object.

delete &NonZero_Locations;

Generally, in C++, you only delete dynamically allocated objects. Stack objects are destroyed when leaving their scope. So, in your case you do not have to (and you're not allowed to) delete NonZero_Locations.

Upvotes: 1

Related Questions