Skip to content

Commit bea14c4

Browse files
committed
Fixed an issue with the output streams not being properly cleaned up when an error occurs. This is now the responsibility of the muxer, as it should be.
1 parent 0bdebd7 commit bea14c4

File tree

3 files changed

+18
-25
lines changed

3 files changed

+18
-25
lines changed

source/ffmpeg-cpp/ffmpeg-cpp/Frame Sinks/AudioEncoder.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,6 @@ namespace ffmpegcpp
7575
delete codec;
7676
codec = nullptr;
7777
}
78-
if (output != nullptr)
79-
{
80-
delete output;
81-
output = nullptr;
82-
}
8378
if (oneInputFrameSink != nullptr)
8479
{
8580
delete oneInputFrameSink;

source/ffmpeg-cpp/ffmpeg-cpp/Frame Sinks/VideoEncoder.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,6 @@ namespace ffmpegcpp
6262
delete formatConverter;
6363
formatConverter = nullptr;
6464
}
65-
if (output != nullptr)
66-
{
67-
delete output;
68-
output = nullptr;
69-
}
7065
if (oneInputFrameSink != nullptr)
7166
{
7267
delete oneInputFrameSink;

source/ffmpeg-cpp/ffmpeg-cpp/Muxing/Muxer.cpp

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,19 @@ namespace ffmpegcpp
3636

3737
void Muxer::CleanUp()
3838
{
39-
if (containerContext != nullptr)
39+
// see if we were primed - we will use this info below
40+
bool wasPrimed = IsPrimed();
41+
42+
// clean up all the output streams that were added to this muxer.
43+
for (int i = 0; i < outputStreams.size(); ++i)
4044
{
41-
// if some of the output streams weren't primed, we cannot finish this process
42-
if (!IsPrimed())
43-
{
44-
throw FFmpegException("You cannot close a muxer when one of the streams wasn't primed. You need to make sure all streams are primed before closing the muxer.");
45-
}
45+
delete outputStreams[i];
46+
}
47+
outputStreams.clear();
4648

47-
/* Write the trailer, if any. The trailer must be written before you
48-
* close the CodecContexts open when you wrote the header; otherwise
49-
* av_write_trailer() may try to use memory that was freed on
50-
* av_codec_close(). */
49+
// clean up the container context - this will also finalize the content of the container!
50+
if (containerContext != nullptr && wasPrimed)
51+
{
5152
// If we don't ALWAYS do this, we leak memory!
5253
av_write_trailer(containerContext);
5354

@@ -58,10 +59,6 @@ namespace ffmpegcpp
5859

5960
avformat_free_context(containerContext);
6061
containerContext = nullptr;
61-
62-
// when the container is closed, the related output streams are closed as well,
63-
// so we clean those up.
64-
outputStreams.clear();
6562
}
6663

6764
// clean up the queue
@@ -206,6 +203,12 @@ namespace ffmpegcpp
206203

207204
void Muxer::Close()
208205
{
206+
// if some of the output streams weren't primed, we cannot finish this process
207+
if (!IsPrimed())
208+
{
209+
throw FFmpegException("You cannot close a muxer when one of the streams wasn't primed. You need to make sure all streams are primed before closing the muxer.");
210+
}
211+
209212
// Make sure we drain all the output streams before we write the first packet.
210213
// We must be sure to do this because in an extreme case, one entire stream
211214
// might be queueing all its packets before we are opened, so it might not
@@ -218,4 +221,4 @@ namespace ffmpegcpp
218221
// free the stream
219222
CleanUp();
220223
}
221-
}
224+
}

0 commit comments

Comments
 (0)