Skip to content

Commit 88af737

Browse files
author
Clement Champetier
committed
AudioFrame: fix memory leak when allocating a new audio buffer
See ffmpeg doc: https://www.ffmpeg.org/doxygen/2.7/group__lavu__sampmanip.html#ga4db4c77f928d32c7d8854732f50b8c04
1 parent 103c33f commit 88af737

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

src/AvTranscoder/data/decoded/AudioFrame.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ std::string AudioFrame::getChannelLayoutDesc() const
5757
return std::string(buf);
5858
}
5959

60+
AudioFrame::~AudioFrame()
61+
{
62+
freeAVSample();
63+
}
64+
6065
size_t AudioFrame::getSize() const
6166
{
6267
if(getSampleFormat() == AV_SAMPLE_FMT_NONE)
@@ -104,6 +109,11 @@ void AudioFrame::allocateAVSample(const AudioFrameDesc& desc)
104109
}
105110
}
106111

112+
void AudioFrame::freeAVSample()
113+
{
114+
av_freep(&_frame->data[0]);
115+
}
116+
107117
void AudioFrame::assign(const unsigned char value)
108118
{
109119
// Create the audio buffer

src/AvTranscoder/data/decoded/AudioFrame.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class AvExport AudioFrame : public Frame
4141
*/
4242
AudioFrame(const AudioFrameDesc& ref);
4343
AudioFrame(const Frame& otherFrame);
44+
~AudioFrame();
4445

4546
size_t getSampleRate() const { return av_frame_get_sample_rate(_frame); }
4647
size_t getNbChannels() const { return av_frame_get_channels(_frame); }
@@ -69,9 +70,16 @@ class AvExport AudioFrame : public Frame
6970
private:
7071
/**
7172
* @brief Allocate the audio buffer of the frame.
73+
* @warning The allocated data should be freed by the caller.
74+
* @see freeAVSample
7275
*/
7376
void allocateAVSample(const AudioFrameDesc& ref);
7477

78+
/**
79+
* @brief Free the audio buffer of the frame.
80+
*/
81+
void freeAVSample();
82+
7583
/**
7684
* @note To allocate new audio buffer if needed.
7785
* @see allocateAVSample

0 commit comments

Comments
 (0)