Skip to content

Commit

Permalink
Removed pointer types for std::vector
Browse files Browse the repository at this point in the history
Note: m_pFifoChunkBuffer was actualy an array pointer; was not deleted
safely. Refactor fixes memory leak.
  • Loading branch information
tcoyvwac committed Sep 30, 2021
1 parent 431d107 commit 21420a8
Show file tree
Hide file tree
Showing 19 changed files with 103 additions and 119 deletions.
21 changes: 9 additions & 12 deletions src/analyzer/analyzergain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,11 @@

AnalyzerGain::AnalyzerGain(UserSettingsPointer pConfig)
: m_rgSettings(pConfig),
m_pLeftTempBuffer(nullptr),
m_pRightTempBuffer(nullptr),
m_iBufferSize(0) {
m_pReplayGain = new ReplayGain();
}

AnalyzerGain::~AnalyzerGain() {
delete[] m_pLeftTempBuffer;
delete[] m_pRightTempBuffer;
delete m_pReplayGain;
}

Expand All @@ -40,15 +36,16 @@ bool AnalyzerGain::processSamples(const CSAMPLE *pIn, const int iLen) {

int halfLength = static_cast<int>(iLen / 2);
if (halfLength > m_iBufferSize) {
delete[] m_pLeftTempBuffer;
delete[] m_pRightTempBuffer;
m_pLeftTempBuffer = new CSAMPLE[halfLength];
m_pRightTempBuffer = new CSAMPLE[halfLength];
m_pLeftTempBuffer = std::vector<CSAMPLE>(halfLength);
m_pRightTempBuffer = std::vector<CSAMPLE>(halfLength);
}
SampleUtil::deinterleaveBuffer(m_pLeftTempBuffer, m_pRightTempBuffer, pIn, halfLength);
SampleUtil::applyGain(m_pLeftTempBuffer, 32767, halfLength);
SampleUtil::applyGain(m_pRightTempBuffer, 32767, halfLength);
return m_pReplayGain->process(m_pLeftTempBuffer, m_pRightTempBuffer, halfLength);
SampleUtil::deinterleaveBuffer(m_pLeftTempBuffer.data(),
m_pRightTempBuffer.data(),
pIn,
halfLength);
SampleUtil::applyGain(m_pLeftTempBuffer.data(), 32767, halfLength);
SampleUtil::applyGain(m_pRightTempBuffer.data(), 32767, halfLength);
return m_pReplayGain->process(m_pLeftTempBuffer.data(), m_pRightTempBuffer.data(), halfLength);
}

void AnalyzerGain::storeResults(TrackPointer tio) {
Expand Down
6 changes: 4 additions & 2 deletions src/analyzer/analyzergain.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

#pragma once

#include <vector>

#include "analyzer/analyzer.h"
#include "preferences/replaygainsettings.h"

Expand All @@ -30,8 +32,8 @@ class AnalyzerGain : public Analyzer {

private:
ReplayGainSettings m_rgSettings;
CSAMPLE* m_pLeftTempBuffer;
CSAMPLE* m_pRightTempBuffer;
std::vector<CSAMPLE> m_pLeftTempBuffer;
std::vector<CSAMPLE> m_pRightTempBuffer;
ReplayGain* m_pReplayGain;
int m_iBufferSize;
};
23 changes: 9 additions & 14 deletions src/effects/lv2/lv2effectprocessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ LV2EffectProcessor::LV2EffectProcessor(EngineEffect* pEngineEffect,
m_audioPortIndices(audioPortIndices),
m_controlPortIndices(controlPortIndices),
m_pEffectsManager(nullptr) {
m_inputL = new float[MAX_BUFFER_LEN];
m_inputR = new float[MAX_BUFFER_LEN];
m_outputL = new float[MAX_BUFFER_LEN];
m_outputR = new float[MAX_BUFFER_LEN];
m_params = new float[pManifest->parameters().size()];
m_inputL = std::vector<CSAMPLE>(MAX_BUFFER_LEN);
m_inputR = std::vector<CSAMPLE>(MAX_BUFFER_LEN);
m_outputL = std::vector<CSAMPLE>(MAX_BUFFER_LEN);
m_outputR = std::vector<CSAMPLE>(MAX_BUFFER_LEN);
m_params = std::vector<float>(pManifest->parameters().size());

const QList<EffectManifestParameterPointer>& effectManifestParameterList =
pManifest->parameters();
Expand Down Expand Up @@ -51,11 +51,6 @@ LV2EffectProcessor::~LV2EffectProcessor() {
}
m_channelStateMatrix.clear();

delete[] m_inputL;
delete[] m_inputR;
delete[] m_outputL;
delete[] m_outputR;
delete[] m_params;
}

void LV2EffectProcessor::initialize(
Expand Down Expand Up @@ -148,10 +143,10 @@ LV2EffectGroupState* LV2EffectProcessor::createGroupState(const mixxx::EnginePar

// We assume the audio ports are in the following order:
// input_left, input_right, output_left, output_right
lilv_instance_connect_port(handle, m_audioPortIndices[0], m_inputL);
lilv_instance_connect_port(handle, m_audioPortIndices[1], m_inputR);
lilv_instance_connect_port(handle, m_audioPortIndices[2], m_outputL);
lilv_instance_connect_port(handle, m_audioPortIndices[3], m_outputR);
lilv_instance_connect_port(handle, m_audioPortIndices[0], m_inputL.data());
lilv_instance_connect_port(handle, m_audioPortIndices[1], m_inputR.data());
lilv_instance_connect_port(handle, m_audioPortIndices[2], m_outputL.data());
lilv_instance_connect_port(handle, m_audioPortIndices[3], m_outputR.data());

lilv_instance_activate(handle);
}
Expand Down
12 changes: 7 additions & 5 deletions src/effects/lv2/lv2effectprocessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#include <lilv/lilv.h>

#include <vector>

#include "effects/defs.h"
#include "effects/effectmanifest.h"
#include "effects/effectprocessor.h"
Expand Down Expand Up @@ -56,11 +58,11 @@ class LV2EffectProcessor : public EffectProcessor {
LV2EffectGroupState* createGroupState(const mixxx::EngineParameters& bufferParameters);

QList<EngineEffectParameter*> m_parameters;
float* m_inputL;
float* m_inputR;
float* m_outputL;
float* m_outputR;
float* m_params;
std::vector<CSAMPLE> m_inputL;
std::vector<CSAMPLE> m_inputR;
std::vector<CSAMPLE> m_outputL;
std::vector<CSAMPLE> m_outputR;
std::vector<CSAMPLE> m_params;
const LilvPlugin* m_pPlugin;
const QList<int> m_audioPortIndices;
const QList<int> m_controlPortIndices;
Expand Down
13 changes: 5 additions & 8 deletions src/effects/lv2/lv2manifest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ LV2Manifest::LV2Manifest(const LilvPlugin* plug,
lilv_node_free(info);

int numPorts = lilv_plugin_get_num_ports(plug);
m_minimum = new float[numPorts];
m_maximum = new float[numPorts];
m_default = new float[numPorts];
lilv_plugin_get_port_ranges_float(m_pLV2plugin, m_minimum, m_maximum,
m_default);
m_minimum = std::vector<float>(numPorts);
m_maximum = std::vector<float>(numPorts);
m_default = std::vector<float>(numPorts);
lilv_plugin_get_port_ranges_float(
m_pLV2plugin, m_minimum.data(), m_maximum.data(), m_default.data());

// Counters to determine the type of the plug in
int inputPorts = 0;
Expand Down Expand Up @@ -153,9 +153,6 @@ LV2Manifest::LV2Manifest(const LilvPlugin* plug,
}

LV2Manifest::~LV2Manifest() {
delete[] m_minimum;
delete[] m_maximum;
delete[] m_default;
}

EffectManifestPointer LV2Manifest::getEffectManifest() const {
Expand Down
8 changes: 5 additions & 3 deletions src/effects/lv2/lv2manifest.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#include <lilv/lilv.h>

#include <vector>

#include "effects/defs.h"
#include "effects/effectmanifest.h"

Expand Down Expand Up @@ -38,8 +40,8 @@ class LV2Manifest {
QList<int> controlPortIndices;

// Arrays used for storing minimum, maximum and default parameter values
float* m_minimum;
float* m_maximum;
float* m_default;
std::vector<float> m_minimum;
std::vector<float> m_maximum;
std::vector<float> m_default;
Status m_status;
};
16 changes: 6 additions & 10 deletions src/encoder/encoderfdkaac.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,8 @@ EncoderFdkAac::EncoderFdkAac(EncoderCallback* pCallback)
m_pCallback(pCallback),
m_pLibrary(nullptr),
m_pInputFifo(nullptr),
m_pFifoChunkBuffer(nullptr),
m_readRequired(0),
m_aacEnc(),
m_pAacDataBuffer(nullptr),
m_aacInfo(),
m_hasSbr(false) {
// Load the shared library
Expand Down Expand Up @@ -164,8 +162,6 @@ EncoderFdkAac::~EncoderFdkAac() {
kLogger.debug() << "Unloaded libfdk-aac";
}

delete[] m_pAacDataBuffer;
delete m_pFifoChunkBuffer;
delete m_pInputFifo;
}

Expand Down Expand Up @@ -283,7 +279,7 @@ int EncoderFdkAac::initEncoder(mixxx::audio::SampleRate sampleRate, QString* pUs
// This initializes the encoder handle but not the encoder itself.
// Actual encoder init is done below.
aacEncOpen(&m_aacEnc, 0, m_channels);
m_pAacDataBuffer = new unsigned char[kOutBufferBits * m_channels]();
m_pAacDataBuffer = std::vector<unsigned char>(kOutBufferBits * m_channels);

// AAC Object Type: specifies "mode": AAC-LC, HE-AAC, HE-AACv2, DAB AAC, etc...
if (aacEncoder_SetParam(m_aacEnc, AACENC_AOT, m_aacAot) != AACENC_OK) {
Expand Down Expand Up @@ -347,7 +343,7 @@ int EncoderFdkAac::initEncoder(mixxx::audio::SampleRate sampleRate, QString* pUs
// the Live Broadcasting implementation
m_pInputFifo = new FIFO<SAMPLE>(EngineSideChain::SIDECHAIN_BUFFER_SIZE * 2);

m_pFifoChunkBuffer = new SAMPLE[m_readRequired * sizeof(SAMPLE)]();
m_pFifoChunkBuffer = std::vector<SAMPLE>(m_readRequired * sizeof(SAMPLE));
return 0;
}

Expand Down Expand Up @@ -382,12 +378,12 @@ void EncoderFdkAac::encodeBuffer(const CSAMPLE* samples, const int sampleCount)
}

void EncoderFdkAac::processFIFO() {
if (!m_pInputFifo || !m_pFifoChunkBuffer) {
if (!m_pInputFifo || !m_pFifoChunkBuffer.data()) {
return;
}

while (m_pInputFifo->readAvailable() >= m_readRequired) {
m_pInputFifo->read(m_pFifoChunkBuffer, m_readRequired);
m_pInputFifo->read(m_pFifoChunkBuffer.data(), m_readRequired);

// fdk-aac only accept pointers for most buffer settings.
// Declare settings here and point to them below.
Expand All @@ -414,7 +410,7 @@ void EncoderFdkAac::processFIFO() {
// Output (result) Buffer
AACENC_BufDesc outputBuf;
outputBuf.numBufs = 1;
outputBuf.bufs = (void**)&m_pAacDataBuffer;
outputBuf.bufs = reinterpret_cast<void**>(m_pAacDataBuffer.data());
outputBuf.bufSizes = &outDataSize;
outputBuf.bufElSizes = &outElemSize;
outputBuf.bufferIdentifiers = &outDataDescription;
Expand All @@ -433,7 +429,7 @@ void EncoderFdkAac::processFIFO() {
kLogger.warning() << "encoder ignored" << sampleDiff << "samples!";
}

m_pCallback->write(nullptr, m_pAacDataBuffer, 0, outputDesc.numOutBytes);
m_pCallback->write(nullptr, m_pAacDataBuffer.data(), 0, outputDesc.numOutBytes);
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/encoder/encoderfdkaac.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <QLibrary>
#include <QString>
#include <memory>
#include <vector>

#include "encoder/encoder.h"
#include "util/fifo.h"
Expand Down Expand Up @@ -205,10 +206,10 @@ class EncoderFdkAac : public Encoder {
EncoderCallback* m_pCallback;
std::unique_ptr<QLibrary> m_pLibrary;
FIFO<SAMPLE>* m_pInputFifo;
SAMPLE* m_pFifoChunkBuffer;
std::vector<SAMPLE> m_pFifoChunkBuffer;
int m_readRequired;
HANDLE_AACENCODER m_aacEnc;
unsigned char* m_pAacDataBuffer;
std::vector<unsigned char> m_pAacDataBuffer;
AACENC_InfoStruct m_aacInfo;
bool m_hasSbr;
};
21 changes: 10 additions & 11 deletions src/test/analyserwaveformtest.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
#include <gtest/gtest.h>

#include <QDir>
#include <QtDebug>

#include "test/mixxxtest.h"
#include <vector>

#include "analyzer/analyzerwaveform.h"
#include "library/dao/analysisdao.h"
#include "test/mixxxtest.h"
#include "track/track.h"

#define BIGBUF_SIZE (1024 * 1024) //Megabyte
Expand All @@ -19,8 +20,8 @@ class AnalyzerWaveformTest : public MixxxTest {
protected:
AnalyzerWaveformTest()
: aw(config(), QSqlDatabase()),
bigbuf(nullptr),
canaryBigBuf(nullptr) {
bigbuf(),
canaryBigBuf() {
}

void SetUp() override {
Expand All @@ -31,14 +32,14 @@ class AnalyzerWaveformTest : public MixxxTest {
mixxx::audio::Bitrate(),
mixxx::Duration::fromMillis(1000));

bigbuf = new CSAMPLE[BIGBUF_SIZE];
bigbuf = std::vector<CSAMPLE>(BIGBUF_SIZE);
for (int i = 0; i < BIGBUF_SIZE; i++)
bigbuf[i] = MAGIC_FLOAT;

//Memory layout for canaryBigBuf looks like
// [ canary | big buf | canary ]

canaryBigBuf = new CSAMPLE[BIGBUF_SIZE + 2 * CANARY_SIZE];
canaryBigBuf = std::vector<CSAMPLE>(BIGBUF_SIZE + 2 * CANARY_SIZE);
for (int i = 0; i < CANARY_SIZE; i++)
canaryBigBuf[i] = CANARY_FLOAT;
for (int i = CANARY_SIZE; i < CANARY_SIZE + BIGBUF_SIZE; i++)
Expand All @@ -48,21 +49,19 @@ class AnalyzerWaveformTest : public MixxxTest {
}

void TearDown() override {
delete[] bigbuf;
delete[] canaryBigBuf;
}

protected:
AnalyzerWaveform aw;
TrackPointer tio;
CSAMPLE* bigbuf;
CSAMPLE* canaryBigBuf;
std::vector<CSAMPLE> bigbuf;
std::vector<CSAMPLE> canaryBigBuf;
};

//Test to make sure we don't modify the source buffer.
TEST_F(AnalyzerWaveformTest, simpleAnalyze) {
aw.initialize(tio, tio->getSampleRate(), BIGBUF_SIZE);
aw.processSamples(bigbuf, BIGBUF_SIZE);
aw.processSamples(bigbuf.data(), BIGBUF_SIZE);
aw.storeResults(tio);
aw.cleanup();
for (int i = 0; i < BIGBUF_SIZE; i++) {
Expand Down
9 changes: 5 additions & 4 deletions src/test/analyzersilence_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#include <gtest/gtest.h>

#include <vector>

#include "engine/engine.h"
#include "test/mixxxtest.h"
#include "track/track.h"
Expand All @@ -27,24 +29,23 @@ class AnalyzerSilenceTest : public MixxxTest {
mixxx::Duration::fromSeconds(kTrackLengthFrames / 44100.0));

nTrackSampleDataLength = kChannelCount * kTrackLengthFrames;
pTrackSampleData = new CSAMPLE[nTrackSampleDataLength];
pTrackSampleData = std::vector<CSAMPLE>(nTrackSampleDataLength);
}

void TearDown() override {
delete[] pTrackSampleData;
}

void analyzeTrack() {
analyzerSilence.initialize(pTrack, pTrack->getSampleRate(), nTrackSampleDataLength);
analyzerSilence.processSamples(pTrackSampleData, nTrackSampleDataLength);
analyzerSilence.processSamples(pTrackSampleData.data(), nTrackSampleDataLength);
analyzerSilence.storeResults(pTrack);
analyzerSilence.cleanup();
}

protected:
AnalyzerSilence analyzerSilence;
TrackPointer pTrack;
CSAMPLE* pTrackSampleData;
std::vector<CSAMPLE> pTrackSampleData;
int nTrackSampleDataLength; // in samples
};

Expand Down
Loading

0 comments on commit 21420a8

Please sign in to comment.