Skip to content

Commit

Permalink
valgrind fix
Browse files Browse the repository at this point in the history
  • Loading branch information
randaz81 committed Aug 1, 2023
1 parent 6699547 commit 112a889
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 15 deletions.
41 changes: 26 additions & 15 deletions src/libYARP_sig/src/yarp/sig/Sound.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,12 +215,7 @@ Sound Sound::subSound(size_t first_sample, size_t last_sample)

void Sound::init(size_t bytesPerSample)
{
if (implementation != nullptr)
{
delete implementation;
implementation = nullptr;
}

delete_implementation();
if (bytesPerSample == 1)
{
implementation = new std::vector<NetUint8>;
Expand All @@ -241,22 +236,38 @@ void Sound::init(size_t bytesPerSample)
this->m_bytesPerSample = bytesPerSample;
}

Sound::~Sound()
void Sound::delete_implementation()
{
if (implementation!=nullptr)
if (implementation != nullptr)
{
delete implementation;
implementation = nullptr;
if (m_bytesPerSample == 1)
{
std::vector<NetUint8>*p = (std::vector<NetUint8>*)(implementation);
delete p;
implementation = nullptr;
}
else if (m_bytesPerSample == 2)
{
std::vector<NetUint16>* p = (std::vector<NetUint16>*)(implementation);
delete p;
implementation = nullptr;
}
else
{
yCError(SOUND, "sound only implemented for 8-16 bit samples");
yCAssert(SOUND, false); // that's all that's implemented right now
}
}
}

Sound::~Sound()
{
delete_implementation();
}

void Sound::resize(size_t samples, size_t channels)
{
if (implementation != nullptr)
{
delete implementation;
implementation = nullptr;
}
delete_implementation();
if (m_bytesPerSample == 1)
{
implementation = new std::vector<NetUint8>;
Expand Down
2 changes: 2 additions & 0 deletions src/libYARP_sig/src/yarp/sig/Sound.h
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,8 @@ class YARP_sig_API Sound : public yarp::os::Portable
*/
size_t getRawDataSize() const;

void delete_implementation();

public:
bool read(yarp::os::ConnectionReader& connection) override;

Expand Down

0 comments on commit 112a889

Please sign in to comment.