Skip to content

Commit ccdbbea

Browse files
author
Clement Champetier
committed
FormatContext: fix memory leak when adding new streams to an OutputFile
See the ffmpeg/libav documentation of avformat_new_stream function: https://www.ffmpeg.org/doxygen/2.7/group__lavf__core.html#gadcb0fd3e507d9b58fe78f61f8ad39827
1 parent a95aa18 commit ccdbbea

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

src/AvTranscoder/file/FormatContext.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ namespace avtranscoder
88

99
FormatContext::FormatContext(const std::string& filename, int req_flags, AVDictionary** options)
1010
: _avFormatContext(NULL)
11+
, _avStreamAllocated()
1112
, _flags(req_flags)
1213
, _options()
1314
, _isOpen(false)
@@ -31,6 +32,7 @@ FormatContext::FormatContext(const std::string& filename, int req_flags, AVDicti
3132

3233
FormatContext::FormatContext(int req_flags)
3334
: _avFormatContext(NULL)
35+
, _avStreamAllocated()
3436
, _flags(req_flags)
3537
, _options()
3638
, _isOpen(false)
@@ -45,8 +47,8 @@ FormatContext::~FormatContext()
4547
return;
4648

4749
// free the streams added
48-
for(unsigned int i = 0; i < _avFormatContext->nb_streams; ++i)
49-
avcodec_close(_avFormatContext->streams[i]->codec);
50+
for(std::vector<AVStream*>::iterator it = _avStreamAllocated.begin(); it != _avStreamAllocated.end(); ++it)
51+
avcodec_close((*it)->codec);
5052

5153
// free the format context
5254
if(_isOpen)
@@ -143,6 +145,7 @@ AVStream& FormatContext::addAVStream(const AVCodec& avCodec)
143145
{
144146
throw std::runtime_error("Unable to add new video stream");
145147
}
148+
_avStreamAllocated.push_back(stream);
146149
return *stream;
147150
}
148151

src/AvTranscoder/file/FormatContext.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ class AvExport FormatContext
122122

123123
private:
124124
AVFormatContext* _avFormatContext; ///< Has ownership
125+
std::vector<AVStream*> _avStreamAllocated; ///< Has link (no ownership)
125126
const int _flags; ///< Flags with which the options are loaded (see AV_OPT_FLAG_xxx)
126127
OptionMap _options;
127128
bool _isOpen; ///< Is the AVFormatContext open (in constructor with a filename)

0 commit comments

Comments
 (0)