为什么填充此数组segfault?

时间:2012-07-30 07:19:25

标签: android c android-ndk java-native-interface

我在使用以下代码时遇到了一些麻烦,这些代码似乎在指定的行中导致了分段错误。我正在尝试创建一个8位无符号整数数组,以便实例化一个OpenCV Mat对象,但是段填充发生在填充数组的循环的中途。

它似乎每次都发生在不同的迭代,导致我怀疑某些东西被GC取消分配,但我无法确定是什么。

SignDetector.c

JNIEXPORT void JNICALL Java_org_xxx_detectBlobs(JNIEnv *env, jclass clazz, jintArray in)
{
    jint *contents = (*env)->GetIntArrayElements(env, in, NULL);

    threshold(contents, PIXEL_SAMPLE_RATE);
    detectBlobs(contents);

    (*env)->ReleaseIntArrayElements(env, in, contents, 0);
}

BlobDetector.cpp

void detectBlobs(jint *contents)
{
    LOGD("Call to detectBlobs in BlobDetector.cpp");

    uint8_t *thresholded = (uint8_t*) malloc(frame_size);

    int i;
    for(i = 0; i < frame_size - 1; i++)
        thresholded[i] = (contents[i] == WHITE) ? 0 : 1; // Segfaults partway through this loop.

frame_size只是图像中像素的数量,也相当于将图像传递给本机代码的jintArray的长度。

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

通过在Android模拟器上重启我的AVD来管理解决此问题。问题似乎也不会发生在真实设备上,所以我只能得出结论,虚拟设备的RAM中出现了爆炸。

相关问题