Skip to content

Commit f0b44c5

Browse files
authored
Merge pull request #909 from OpenShot/memory-leak-mar-20-2023
Fix Memory leaks in FFmpegReader, Clip, and FrameMapper
2 parents ec0c0b1 + 59e0862 commit f0b44c5

File tree

3 files changed

+15
-8
lines changed

3 files changed

+15
-8
lines changed

src/Clip.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,6 @@ void Clip::init_settings()
9595

9696
// Init reader info struct
9797
init_reader_settings();
98-
99-
// Init cache
100-
final_cache.SetMaxBytesFromInfo(10, info.width, info.height, info.sample_rate, info.channels);
10198
}
10299

103100
// Init reader info details
@@ -108,6 +105,9 @@ void Clip::init_reader_settings() {
108105

109106
// Initialize info struct
110107
info = reader->info;
108+
109+
// Init cache
110+
final_cache.SetMaxBytesFromInfo(8, info.width, info.height, info.sample_rate, info.channels);
111111
}
112112
}
113113

src/FFmpegReader.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1359,6 +1359,9 @@ void FFmpegReader::ProcessVideoPacket(int64_t requested_frame) {
13591359
// Check if the AVFrame is finished and set it
13601360
if (!frame_finished) {
13611361
// No AVFrame decoded yet, bail out
1362+
if (pFrame) {
1363+
RemoveAVFrame(pFrame);
1364+
}
13621365
return;
13631366
}
13641367

@@ -1383,8 +1386,6 @@ void FFmpegReader::ProcessVideoPacket(int64_t requested_frame) {
13831386
int height = info.height;
13841387
int width = info.width;
13851388
int64_t video_length = info.video_length;
1386-
AVFrame *my_frame = pFrame;
1387-
pFrame = NULL;
13881389

13891390
// Create variables for a RGB Frame (since most videos are not in RGB, we must convert it)
13901391
AVFrame *pFrameRGB = nullptr;
@@ -1485,7 +1486,7 @@ void FFmpegReader::ProcessVideoPacket(int64_t requested_frame) {
14851486
height, PIX_FMT_RGBA, scale_mode, NULL, NULL, NULL);
14861487

14871488
// Resize / Convert to RGB
1488-
sws_scale(img_convert_ctx, my_frame->data, my_frame->linesize, 0,
1489+
sws_scale(img_convert_ctx, pFrame->data, pFrame->linesize, 0,
14891490
original_height, pFrameRGB->data, pFrameRGB->linesize);
14901491

14911492
// Create or get the existing frame object
@@ -1510,7 +1511,7 @@ void FFmpegReader::ProcessVideoPacket(int64_t requested_frame) {
15101511
AV_FREE_FRAME(&pFrameRGB);
15111512

15121513
// Remove frame and packet
1513-
RemoveAVFrame(my_frame);
1514+
RemoveAVFrame(pFrame);
15141515
sws_freeContext(img_convert_ctx);
15151516

15161517
// Get video PTS in seconds
@@ -1599,6 +1600,11 @@ void FFmpegReader::ProcessAudioPacket(int64_t requested_frame) {
15991600

16001601
// Calculate total number of samples
16011602
packet_samples = audio_frame->nb_samples * AV_GET_CODEC_ATTRIBUTES(aStream, aCodecCtx)->channels;
1603+
} else {
1604+
if (audio_frame) {
1605+
// Free audio frame
1606+
AV_FREE_FRAME(&audio_frame);
1607+
}
16021608
}
16031609

16041610
// Estimate the # of samples and the end of this packet's location (to prevent GAPS for the next timestamp)

src/FrameMapper.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,8 @@ std::shared_ptr<Frame> FrameMapper::GetFrame(int64_t requested_frame)
491491
if (info.sample_rate == mapped_frame->SampleRate() &&
492492
info.channels == mapped_frame->GetAudioChannelsCount() &&
493493
info.channel_layout == mapped_frame->ChannelsLayout() &&
494-
mapped.Samples.total == mapped_frame->GetAudioSamplesCount() == samples_in_frame && is_increasing &&
494+
mapped.Samples.total == mapped_frame->GetAudioSamplesCount() &&
495+
mapped.Samples.total == samples_in_frame && is_increasing &&
495496
mapped.Samples.frame_start == mapped.Odd.Frame &&
496497
mapped.Samples.sample_start == 0 &&
497498
mapped_frame->number == frame_number &&// in some conditions (e.g. end of stream)

0 commit comments

Comments
 (0)