ffmpeg每次打开麦克风和扬声器设备都会有大小不等的内存泄漏
ffmpeg吧
全部回复
仅看楼主
level 1
老王_子 楼主
/* 打开音频捕获 */
BOOL OpenAudioCapture()
{
std::string audioname;
int namesize = 0;
char cadname[1024];
if (!AudioDeviceCheck(audioname))
{
TestFlag = 110;
return FALSE;
}
//查找输入方式
const AVInputFormat* pAudioInputFmt = av_find_input_format("dshow");
namesize = sprintf_s(cadname, 1024, "audio=%s\0", audioname.c_str());
const char* psDevName = cadname;
AVDictionary* options = nullptr;
// 设置选项(可选)
av_dict_set(&options, "sample_rate", "44100", 0); // 采样率
av_dict_set(&options, "channels", "2", 0); // 声道数
av_dict_set(&options, "sample_fmt", "s16le", 0); // 采样格式
//以Direct Show的方式打开设备,并将 输入方式 关联到格式上下文
if (avformat_open_input(&pFormatCtx_Audio, cadname, pAudioInputFmt, &options) < 0)
{
TestFlag = 11;
return FALSE;
}
ffmpegres.pFormatCtx_Audio = TRUE;
av_dict_free(&options);
if (avformat_find_stream_info(pFormatCtx_Audio, NULL) < 0)
{
TestFlag = 19;
return FALSE;
}
if (pFormatCtx_Audio->streams[0]->codecpar->codec_type != AVMEDIA_TYPE_AUDIO)
{
TestFlag = 12;
return FALSE;
}
const AVCodec* tmpCodec = avcodec_find_decoder(pFormatCtx_Audio->streams[0]->codecpar->codec_id);
pReadCodecContext_Audio = avcodec_alloc_context3(tmpCodec);
if (NULL == pReadCodecContext_Audio)
{
TestFlag = 13;
return FALSE;
}
ffmpegres.pReadCodecContext_Audio = TRUE;
pReadCodecContext_Audio->sample_rate = select_sample_rate(tmpCodec);
pReadCodecContext_Audio->channel_layout = select_channel_layout(tmpCodec);
pReadCodecContext_Audio->channels = av_get_channel_layout_nb_channels(pReadCodecContext_Audio->channel_layout);
pReadCodecContext_Audio->sample_fmt = (AVSampleFormat)pFormatCtx_Audio->streams[0]->codecpar->format;
if (0 > avcodec_open2(pReadCodecContext_Audio, tmpCodec, NULL))
{
TestFlag = 14;
return FALSE;
}
return TRUE;
}
我的扬声器设备打开的方式与麦克风一样,我有仔细检查过每个创建的指针都有回收。但是依旧还是会出现内存泄漏的问。
2025年07月14日 09点07分 1
level 1
老王_子 楼主
这个是我的资源回收函数
void VideoRecycle()
{
if (ffmpegres.pReadCodecCtx_Video)
{
avcodec_free_context(&pReadCodecCtx_Video);
pReadCodecCtx_Video = NULL; // 避免悬空指针
}
if (ffmpegres.pFormatCtx_Video)
{
if (pFormatCtx_Video != NULL)
{
avformat_close_input(&pFormatCtx_Video);
pFormatCtx_Video = NULL; // 避免悬空指针
}
}
if (ffmpegres.pReadCodecContext_Audio)
{
avcodec_free_context(&pReadCodecContext_Audio);
pReadCodecContext_Audio = NULL; // 避免悬空指针
}
if (ffmpegres.pFormatCtx_Audio)
{
if (pFormatCtx_Audio != NULL)
{
avformat_close_input(&pFormatCtx_Audio);
pFormatCtx_Audio = NULL; // 避免悬空指针
}
}
if (ffmpegres.pReadCodecContext_Speak)
{
avcodec_free_context(&pReadCodecContext_Speak);
pReadCodecContext_Speak = NULL; // 避免悬空指针
}
if (ffmpegres.pFormatCtx_Speak)
{
if (pFormatCtx_Speak != NULL)
{
avformat_close_input(&pFormatCtx_Speak);
pFormatCtx_Speak = NULL; // 避免悬空指针
}
}
if (ffmpegres.pFormatCtx_Out)
{
if (pFormatCtx_Out != NULL)
{
if(!(pFormatCtx_Out->oformat->flags & AVFMT_NOFILE))
avio_closep(&pFormatCtx_Out->pb);
avformat_free_context(pFormatCtx_Out);
pFormatCtx_Out = NULL; // 避免悬空指针
}
}
if (ffmpegres.pCodecEncodeCtx_Video)
{
avcodec_free_context(&pCodecEncodeCtx_Video);
pCodecEncodeCtx_Video = NULL; // 避免悬空指针
}
if (ffmpegres.pCodecEncodeCtx_Audio)
{
avcodec_free_context(&pCodecEncodeCtx_Audio);
pCodecEncodeCtx_Audio = NULL; // 避免悬空指针
}
if (ffmpegres.fifo_video)
{
av_fifo_free(fifo_video);
fifo_video = NULL; // 避免悬空指针
}
if (ffmpegres.fifo_audio_mic)
{
av_audio_fifo_free(fifo_audio_mic);
fifo_audio_mic = NULL; // 避免悬空指针
}
if (ffmpegres.fifo_speak)
{
av_audio_fifo_free(fifo_speak);
fifo_speak = NULL; // 避免悬空指针
}
if (ffmpegres.inputs)
{
avfilter_inout_free(&inputs);
inputs = NULL;
}
if (ffmpegres.outputs)
{
avfilter_inout_free(&outputs);
outputs = NULL;
}
if (ffmpegres.filterGraph)
{
avfilter_graph_free(&filterGraph);
filterGraph = NULL;
}
if (ffmpegres._filter_graph)
{
avfilter_graph_free(&_filter_graph);
_filter_graph = NULL;
}
}
2025年07月14日 09点07分 2
level 1
老王_子 楼主
我查看内存使用率就发现每次出现内存泄漏的时候,就是出现在麦克风设备和扬声器设备被打开的时候,与audio_sniffer-x64.dll这个动态库有关系,但是不清楚应该怎么解决。
2025年07月14日 09点07分 3
level 7
盲猜 cadname字符串没有回收
2025年07月16日 06点07分 4
1