Java 当我将代码拆分为两个函数时,JNI崩溃

Java 当我将代码拆分为两个函数时,JNI崩溃,java,c,ffmpeg,java-native-interface,Java,C,Ffmpeg,Java Native Interface,我有一个正常工作的本机C函数,我从java代码中调用它。但当我把代码分成两个函数并依次调用它们时,我得到了一个致命的错误 //global variables AVFormatContext *pFormatCtx; AVFrame *pFrame; AVFrame *pFrameRGB; AVCodecContext *pCodecCtx; AVCodec *pCodec; uint8_t *buffer; int

我有一个正常工作的本机C函数,我从java代码中调用它。但当我把代码分成两个函数并依次调用它们时,我得到了一个致命的错误

//global variables
AVFormatContext *pFormatCtx;
AVFrame         *pFrame;
AVFrame         *pFrameRGB;
AVCodecContext  *pCodecCtx;
AVCodec         *pCodec;
uint8_t         *buffer;
int             videoStream;
struct SwsContext      *sws_ctx = NULL;
int outWidth, outHeight;
工作无约束函数 Java代码
有什么想法吗?

如果没有看到
foo1
foo2
的代码,就没有想法了。。。不知道如果没有这个答案你会怎么想。我可以想象分割一个函数可能会有几十种方法引起问题。@JimGarrison我已经添加了
foo
的完整代码
foo1
包含第一行,
foo2
包含所有其他行。代码中的SEGV PC地址在哪里?您在这里使用多线程吗?
JNIEXPORT void JNICALL Java_foo(JNIEnv * env, jclass class) {
    av_register_all();
    const char* videoPath = "11.mp4";

    int             numBytes;
    AVDictionary    *optionsDict = NULL;

    pFrame = NULL; 
    pFrameRGB = NULL;
    buffer = NULL;
    pCodec = NULL;
    pFormatCtx = NULL;

    // Open video file
    if(avformat_open_input(&pFormatCtx, videoPath, NULL, NULL)!=0)
            exit(1); // Couldn't open file


    // Retrieve stream information
    if(avformat_find_stream_info(pFormatCtx, NULL)<0)
            exit(1); // Couldn't find stream information

     av_dump_format(pFormatCtx, 0,videoPath, 0);


    // Find the first video stream
    videoStream=-1;
    int i;
    for(i=0; i<pFormatCtx->nb_streams; i++) {
            if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO) {
                    videoStream=i;
                    break;
            }
    }

    if(videoStream==-1)
            exit(1); // Didn't find a video stream

    // Get a pointer to the codec context for the video stream
    pCodecCtx=pFormatCtx->streams[videoStream]->codec;


    // Find the decoder for the video stream
    pCodec=avcodec_find_decoder(pCodecCtx->codec_id);
    if(pCodec==NULL) {
            fprintf(stderr, "Unsupported codec!\n");
            exit(1); // Codec not found
    }

    // Open codec
    if(avcodec_open2(pCodecCtx, pCodec, &optionsDict)<0)
            exit(1); // Could not open codec

    // Allocate video frame
    pFrame=avcodec_alloc_frame();

    // Allocate an AVFrame structure
    pFrameRGB=avcodec_alloc_frame();
    if(pFrameRGB==NULL)
            exit(1);

    outWidth = 128;
    outHeight = 128;

    // Determine required buffer size and allocate buffer
    numBytes=avpicture_get_size(PIX_FMT_RGB24, outWidth, outHeight);
    buffer=(uint8_t *)av_malloc(numBytes*sizeof(uint8_t));

    sws_ctx = sws_getContext(
                            pCodecCtx->width,
                            pCodecCtx->height,
                            pCodecCtx->pix_fmt,
                            outWidth,
                            outHeight,
                            PIX_FMT_RGB24,
                            SWS_BILINEAR,
                            NULL,
                            NULL,
                            NULL
                    );

    // Assign appropriate parts of buffer to image planes in pFrameRGB
    // Note that pFrameRGB is an AVFrame, but AVFrame is a superset
    // of AVPicture
    avpicture_fill((AVPicture *)pFrameRGB, buffer, PIX_FMT_RGB24, outWidth, outHeight);
}
JNIEXPORT void JNICALL Java_foo1(JNIEnv * env, jclass class) {
    av_register_all();
}

JNIEXPORT void JNICALL Java_foo2(JNIEnv * env, jclass class) {
    //all lines of code from Java_foo exept the first
}
System.loadLibrary("mylib");
Mylib.foo1();
Mylib.foo2(); //fatal error


#
# A fatal error has been detected by the Java Runtime Environment:
#
#  SIGSEGV (0xb) at pc=0x00007faab5012dc0, pid=15571, tid=140371352766208