SCBufferAdaptor Fix double free by not calling cleanup() in destructor

plus some tidying up
nix
Owen Green 5 years ago
parent bd79448be4
commit 54cda6c78b

@ -25,29 +25,7 @@ under the European Unions Horizon 2020 research and innovation programme
namespace fluid { namespace fluid {
namespace client { namespace client {
/** /*
A descendent of SndBuf that will populate itself
from the NRT mirror buffers given a world and a bufnum
**/
struct NRTBuf
{
NRTBuf(SndBuf* b) : mBuffer(b) {}
NRTBuf(World* world, index bufnum, bool rt = false)
: NRTBuf(rt ? World_GetBuf(world, static_cast<uint32>(bufnum))
: World_GetNRTBuf(world, static_cast<uint32>(bufnum)))
{
if (mBuffer && !static_cast<bool>(mBuffer->samplerate))
mBuffer->samplerate = world->mFullRate.mSampleRate;
}
protected:
SndBuf* mBuffer;
};
/**
A combination of SndBuf and client::BufferAdaptor (which, in turn, exposes
FluidTensorView<float,2>), for simple transfer of data
Given a World* and a buffer number, this will populate its SndBuf stuff Given a World* and a buffer number, this will populate its SndBuf stuff
from the NRT mirror buffers, and create a FluidTensorView wrapper of from the NRT mirror buffers, and create a FluidTensorView wrapper of
appropriate dimensions. appropriate dimensions.
@ -60,24 +38,27 @@ protected:
nSamps = rows nSamps = rows
nChans = columns nChans = columns
**/ **/
class SCBufferAdaptor : public NRTBuf, public client::BufferAdaptor class SCBufferAdaptor;
std::ostream& operator<<(std::ostream& os, SCBufferAdaptor& b);
class SCBufferAdaptor :public client::BufferAdaptor
{ {
public: public:
// SCBufferAdaptor() = delete;
SCBufferAdaptor(const SCBufferAdaptor&) = delete; SCBufferAdaptor(const SCBufferAdaptor&) = delete;
SCBufferAdaptor& operator=(const SCBufferAdaptor&) = delete; SCBufferAdaptor& operator=(const SCBufferAdaptor&) = delete;
SCBufferAdaptor(SCBufferAdaptor&&) = default; SCBufferAdaptor(SCBufferAdaptor&&) = default;
SCBufferAdaptor(SndBuf* buf, World* world, bool local) SCBufferAdaptor(SndBuf* buf, World* world, bool local)
: NRTBuf{buf}, mWorld{world}, mLocal{local} : mBuffer{buf}, mWorld{world}, mLocal{local}
{} {}
SCBufferAdaptor(index bufnum, World* world, bool rt = false) SCBufferAdaptor(index bufnum, World* world)
: NRTBuf(world, bufnum, rt), mBufnum(bufnum), mWorld(world) : mBuffer{World_GetNRTBuf(world, static_cast<uint32>(bufnum))}, mBufnum(bufnum), mWorld(world)
{} {}
~SCBufferAdaptor() { cleanUp(); } // ~SCBufferAdaptor() { cleanUp(); }
void assignToRT(World* rtWorld) void assignToRT(World* rtWorld)
{ {
@ -89,13 +70,10 @@ public:
} }
void cleanUp() void cleanUp()
{
if (mOldData)
{ {
boost::alignment::aligned_free(mOldData); boost::alignment::aligned_free(mOldData);
mOldData = nullptr; mOldData = nullptr;
} }
}
// No locks in (vanilla) SC, so no-ops for these // No locks in (vanilla) SC, so no-ops for these
bool acquire() const override { return true; } bool acquire() const override { return true; }
@ -179,11 +157,9 @@ public:
const Result resize(index frames, index channels, double sampleRate) override const Result resize(index frames, index channels, double sampleRate) override
{ {
SndBuf* thisThing = mBuffer;
if(mLocal) // don't try and resize if(mLocal) // don't try and resize
{ {
if(frames > thisThing->frames || channels > thisThing->channels) if(frames > mBuffer->frames || channels > mBuffer->channels)
{ {
return {Result::Status::kError, "Local buffer must be presized adequetly, need ", return {Result::Status::kError, "Local buffer must be presized adequetly, need ",
frames, " frames, ", channels, " channels." }; frames, " frames, ", channels, " channels." };
@ -191,11 +167,13 @@ public:
else return {}; else return {};
} }
mOldData = thisThing->data; mOldData = mBuffer->data;
int allocResult = int allocResult =
mWorld->ft->fBufAlloc(mBuffer, static_cast<int>(channels), mWorld->ft->fBufAlloc(mBuffer, static_cast<int>(channels),
static_cast<int>(frames), sampleRate); static_cast<int>(frames), sampleRate);
if(mBuffer->data == mOldData) mOldData = nullptr;
Result r; Result r;
if (allocResult != kSCErr_None) if (allocResult != kSCErr_None)
@ -210,8 +188,9 @@ public:
void realTime(bool rt) { mRealTime = rt; } void realTime(bool rt) { mRealTime = rt; }
protected: protected:
SndBuf* mBuffer;
bool mRealTime{false}; bool mRealTime{false};
float* mOldData{0}; float* mOldData{nullptr};
index mBufnum; index mBufnum;
World* mWorld; World* mWorld;
bool mLocal{false}; bool mLocal{false};

Loading…
Cancel
Save