Skip to content

Commit fb7be18

Browse files
committed
fix: memory leak while recording audio
1 parent 4a4adc2 commit fb7be18

File tree

2 files changed

+6
-6
lines changed

2 files changed

+6
-6
lines changed

doc/USE_GPERFTOOLS.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ sudo apt-get install ghostscript graphviz
2929
## 3. Run the Program and Generate Performance Data
3030
Execute program and output the performance data to a specified file, for example:
3131
```bash
32-
CPUPROFILE=./prof.out CPUPROFILESIGNAL=12 ./pi_webrtc --camera=v4l2:0 --fps=30 --width=1280 --height=960 --v4l2_format=h264 --hw_accel --uid=home-pi-3b
32+
CPUPROFILE=./prof.out HEAPPROFILE=heap CPUPROFILESIGNAL=12 ./pi_webrtc --camera=v4l2:0 --fps=30 --width=1280 --height=960 --v4l2_format=h264 --hw_accel --uid=home-pi-3b
3333
```
3434

3535
Send a signal to the process to start/stop collect performance data:
@@ -43,6 +43,7 @@ Convert the performance data into a PDF report using the pprof tool:
4343

4444
```bash
4545
pprof /home/pi/IoT/RaspberryPi_WebRTC/build/pi_webrtc prof.out.0 --pdf > prof_0.pdf
46+
pprof --pdf --base=heap.0001.heap ./test_recorder heap.0003.heap > heap_diff.pdf
4647
```
4748

4849
# Reference

src/recorder/audio_recorder.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ void AudioRecorder::Encode() {
6363
return;
6464
}
6565

66+
AVPacket *pkt = av_packet_alloc();
6667
while (ret >= 0) {
67-
AVPacket *pkt = av_packet_alloc();
6868
ret = avcodec_receive_packet(encoder, pkt);
6969
if (ret == AVERROR(EAGAIN)) {
7070
break;
@@ -79,10 +79,9 @@ void AudioRecorder::Encode() {
7979
pkt->duration = av_rescale_q(pkt->duration, encoder->time_base, st->time_base);
8080

8181
OnPacketed(pkt);
82-
83-
av_packet_unref(pkt);
84-
av_packet_free(&pkt);
8582
}
83+
av_packet_unref(pkt);
84+
av_packet_free(&pkt);
8685
}
8786

8887
void AudioRecorder::OnBuffer(PaBuffer &buffer) {
@@ -106,7 +105,7 @@ void AudioRecorder::OnBuffer(PaBuffer &buffer) {
106105

107106
if (fifo_buffer.write(reinterpret_cast<void **>(converted_input_samples), samples_per_channel) <
108107
samples_per_channel) {
109-
DEBUG_PRINT("Failed to write audio date into fifo buffer.");
108+
DEBUG_PRINT("Failed to write audio data into fifo buffer.");
110109
}
111110

112111
if (converted_input_samples) {

0 commit comments

Comments
 (0)