简体   繁体   中英

Corrupted double-linked list error when running program for the 1st time, subsequent runs are ok

I wrote a Java / JNI wrapper around a camera driver, which I'm using as a unit test for now. There's a strange problem: the first time I run it after turning on or power-cycling the camera, it always has the following error:

*** Error in `java': corrupted double-linked list: 0x00007f70014bf4c0 ***
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x77725)[0x7f70084de725]
/lib/x86_64-linux-gnu/libc.so.6(+0x7daa4)[0x7f70084e4aa4]
/lib/x86_64-linux-gnu/libc.so.6(+0x82175)[0x7f70084e9175]
/lib/x86_64-linux-gnu/libc.so.6(__libc_malloc+0x54)[0x7f70084ea5a4]
/usr/lib/jvm/java-8-oracle/jre/lib/amd64/server/libjvm.so(+0x91bbd5)        [0x7f7007d92bd5]
/usr/lib/jvm/java-8-oracle/jre/lib/amd64/server/libjvm.so(+0x6deab8)[0x7f7007b55ab8]
/home/ceres/git/CameraTest/build/libPleoraWrapper.so(_ZN7JNIEnv_20GetByteArrayElementsEP11_jbyteArrayPh+0x33)    [0x7f6feddfc4e7]
/home/ceres/git/CameraTest/build/libPleoraWrapper.so(Java_net_ceresimaging_camera_pleora_PleoraCamera_nativeCopyRawImage+0x53)[0x7f6feddfbcf4]
[0x7f6ff1017494]

The next time and all the subsequent times it runs fine. This is my C++ code:

JNIEXPORT jint JNICALL Java_net_---------_camera_pleora_PleoraCamera_nativeCopyRawImage
  (JNIEnv * env, jobject obj, jbyteArray arr) {

    int copyLen = 0;

    if (imageSizeBytes>0){

        jbyte* bufferPtr = env->GetByteArrayElements(arr, NULL);//<-- the error seems to be happening here

        jsize len = env->GetArrayLength(arr);

        copyLen = std::min(len,imageSizeBytes);

        std::memcpy(bufferPtr, imageBuffer, copyLen);

        env->ReleaseByteArrayElements(arr,bufferPtr,0);


    }

    return copyLen;

}

I'm fairly certain that I'm passing a valid array. What could be wrong and what's the best way to debug this?

I tried a different method of returning the data: not through an array that's passed as an argument, as described in this question, but by allocating a new array every time in the C++ function, just to see if the first-run crash keeps happening. It did. although I would get a segmentation fault instead of a corrupted double-linked list. I played with the size of the allocated array. After opening the camera for the first time, I was able to allocate a 1000-byte array but not a 2000-byte one (both would be too small).

I was finally able to get around the problem by opening, closing, and reopening the camera every time on startup before trying to allocate a new jbyte array (alternative method) or get write access to it (this question), which probably still involves allocating a new array under the hood, as far as I understand the JNI documentation. Perhaps this is a JVM or a camera driver bug, but I can get around it.

I wonder if there is a way to prevent a crash like this by checking the amount of memory available to the C++ code.

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