Use unique_ptr instead of C-style allocations.

This commit is contained in:
Stephen Anthony 2022-09-18 16:56:57 -02:30
parent 79bf02f5e3
commit 3b073054ff
2 changed files with 8 additions and 10 deletions

View File

@ -476,8 +476,7 @@ void SoundSDL2::stopWav()
} }
if(myWavCvtBuffer) if(myWavCvtBuffer)
{ {
SDL_free(myWavCvtBuffer); myWavCvtBuffer.reset();
myWavCvtBuffer = nullptr;
myWavCvtBufferSize = 0; myWavCvtBufferSize = 0;
} }
} }
@ -510,15 +509,14 @@ void SoundSDL2::wavCallback(void* udata, uInt8* stream, int len)
SDL_assert(cvt.needed); // Obviously, this one is always needed. SDL_assert(cvt.needed); // Obviously, this one is always needed.
cvt.len = len * myWavSpec.channels; // Mono 8 bit sample frames cvt.len = len * myWavSpec.channels; // Mono 8 bit sample frames
if(!myWavCvtBuffer || myWavCvtBufferSize < static_cast<uInt32>(cvt.len * cvt.len_mult)) if(!myWavCvtBuffer ||
myWavCvtBufferSize < static_cast<uInt32>(cvt.len * cvt.len_mult))
{ {
if(myWavCvtBuffer)
SDL_free(myWavCvtBuffer);
myWavCvtBufferSize = cvt.len * cvt.len_mult; myWavCvtBufferSize = cvt.len * cvt.len_mult;
myWavCvtBuffer = static_cast<uInt8*>(SDL_malloc(myWavCvtBufferSize)); myWavCvtBuffer = make_unique<uInt8>(myWavCvtBufferSize);
} }
//cvt.buf = static_cast<uInt8*>(SDL_malloc(cvt.len * cvt.len_mult)); cvt.buf = myWavCvtBuffer.get();
cvt.buf = myWavCvtBuffer;
// Read original data into conversion buffer // Read original data into conversion buffer
SDL_memcpy(cvt.buf, myWavPos, cvt.len); SDL_memcpy(cvt.buf, myWavPos, cvt.len);
SDL_ConvertAudio(&cvt); SDL_ConvertAudio(&cvt);
@ -548,7 +546,7 @@ uInt8* SoundSDL2::myWavPos = nullptr; // pointer to the audio buffer to be playe
uInt32 SoundSDL2::myWavLen = 0; // remaining length of the sample we have to play uInt32 SoundSDL2::myWavLen = 0; // remaining length of the sample we have to play
#ifdef RESAMPLE_WAV #ifdef RESAMPLE_WAV
double SoundSDL2::myWavSpeed = 1.0; double SoundSDL2::myWavSpeed = 1.0;
uInt8* SoundSDL2::myWavCvtBuffer = nullptr; unique_ptr<uInt8> SoundSDL2::myWavCvtBuffer = nullptr;
uInt32 SoundSDL2::myWavCvtBufferSize = 0; uInt32 SoundSDL2::myWavCvtBufferSize = 0;
#endif #endif

View File

@ -196,7 +196,7 @@ class SoundSDL2 : public Sound
uInt8* myWavBuffer{nullptr}; uInt8* myWavBuffer{nullptr};
#ifdef RESAMPLE_WAV #ifdef RESAMPLE_WAV
static double myWavSpeed; static double myWavSpeed;
static uInt8* myWavCvtBuffer; static unique_ptr<uInt8> myWavCvtBuffer;
static uInt32 myWavCvtBufferSize; static uInt32 myWavCvtBufferSize;
#endif #endif