You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
Copy file name to clipboardExpand all lines: source/ffmpeg-cpp/ffmpeg-cpp/Muxing/Muxer.cpp
+18-15Lines changed: 18 additions & 15 deletions
Original file line number
Diff line number
Diff line change
@@ -36,18 +36,19 @@ namespace ffmpegcpp
36
36
37
37
voidMuxer::CleanUp()
38
38
{
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)
40
44
{
41
-
// if some of the output streams weren't primed, we cannot finish this process
42
-
if (!IsPrimed())
43
-
{
44
-
throwFFmpegException("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();
46
48
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
+
{
51
52
// If we don't ALWAYS do this, we leak memory!
52
53
av_write_trailer(containerContext);
53
54
@@ -58,10 +59,6 @@ namespace ffmpegcpp
58
59
59
60
avformat_free_context(containerContext);
60
61
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();
65
62
}
66
63
67
64
// clean up the queue
@@ -206,6 +203,12 @@ namespace ffmpegcpp
206
203
207
204
voidMuxer::Close()
208
205
{
206
+
// if some of the output streams weren't primed, we cannot finish this process
207
+
if (!IsPrimed())
208
+
{
209
+
throwFFmpegException("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
+
209
212
// Make sure we drain all the output streams before we write the first packet.
210
213
// We must be sure to do this because in an extreme case, one entire stream
211
214
// might be queueing all its packets before we are opened, so it might not
0 commit comments