You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

调用avcodec_open2触发System.AccessViolationException问题排查

问题:FFmpeg.AutoGen调用avcodec_open2在NVIDIA GeForce MX350设备上崩溃

错误现象

使用支持Nvidia硬编码的FFmpeg.AutoGen v6.0.0.2,在部分设备(如NVIDIA GeForce MX350)调用ffmpeg.avcodec_open2时,FFmpeg日志输出错误:

[h264_nvenc @ 000001f473fcc480] OpenEncodeSessionEx failed: unsupported device (2): (no details)

随后程序崩溃,抛出异常:

Fatal error. System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
   at FFmpeg.AutoGen.DynamicallyLoadedBindings+<>c.<Initialize>b__2_494(FFmpeg.AutoGen.AVCodecContext*, FFmpeg.AutoGen.AVCodec*, FFmpeg.AutoGen.AVDictionary**)
   at FFmpeg.AutoGen.ffmpeg.avcodec_open2(FFmpeg.AutoGen.AVCodecContext*, FFmpeg.AutoGen.AVCodec*, FFmpeg.AutoGen.AVDictionary**)
   at GraphicsCheck.FfmpegChecker.StartEncoder(Omni.Platforms.Windows.Services.AV.CodecInfo, Int32, Int32, Int32)
   at GraphicsCheck.FfmpegChecker.CheckConfig(CodecConfig)
   at Program.<Main>$(System.String[])

已完成的排查

已创建示例应用复现问题,追踪调用流程如下:

  • 问题触发点为FFmpeg n6.0版本nvenc.c中nvenc_open_session函数(第398行),调用位置在nvenc_check_device方法内(第650行)
  • 错误触发后跳转到fail2处理逻辑(第673行),代码如下:
    fail2:
        CHECK_CU(dl_fn->cuda_dl->cuCtxDestroy(ctx->cu_context_internal));
        ctx->cu_context_internal = NULL;
    
  • ctx->cu_context_internal由同一方法内的cuCtxCreate创建(第640行),推测已正确创建(否则流程不会走到这一步):
    ret = CHECK_CU(dl_fn->cuda_dl->cuCtxCreate(&ctx->cu_context_internal, 0, cu_device));
        if (ret < 0)
            goto fail;
    
  • 此处的ctx是AVCodecContext的priv_data,定义在nvenc_check_device函数中(第606行):
    static av_cold int nvenc_check_device(AVCodecContext *avctx, int idx)
    {
        NvencContext *ctx = avctx->priv_data;
    }
    
  • 打印错误信息的nvenc_print_error函数(第146行)也会操作上下文:
    static int nvenc_print_error(AVCodecContext *avctx, NVENCSTATUS err,
                                 const char *error_string)
    {
        const char *desc;
        const char *details = "(no details)";
        int ret = nvenc_map_error(err, &desc);
    
    #ifdef NVENC_HAVE_GETLASTERRORSTRING
        NvencContext *ctx = avctx->priv_data;
        NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &ctx->nvenc_dload_funcs.nvenc_funcs;
    
        if (p_nvenc && ctx->nvencoder)
            details = p_nvenc->nvEncGetLastErrorString(ctx->nvencoder);
    #endif
    
        av_log(avctx, AV_LOG_ERROR, "%s: %s (%d): %s\n", error_string, desc, err, details);
    
        return ret;
    }
    
  • 日志已输出错误信息,说明av_log执行正常,其他逻辑无问题
  • 错误调用链为nvenc_check_device -> nvenc_setup_device(第778行),但未看到更多日志,推测问题出在nvenc_check_device的fail2处理逻辑或nvenc_print_error中

求助排查思路

恳请提供针对该崩溃问题的排查方向。


内容的提问来源于stack exchange,提问作者forlayo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 16:45:27