AV::Sound copies sound buffer to avoid memory leak

This commit is contained in:
Josh Holtrop 2011-05-21 23:20:50 -04:00
parent 8f1329fed8
commit 278867b3c8
3 changed files with 12 additions and 6 deletions

View File

@ -244,6 +244,7 @@ AV::Sound::Sound(AV & av)
m_ss = NULL;
m_buff_bytes_left = 0;
m_volume = 1.0f;
m_data = NULL;
}
AV::Sound::~Sound()
@ -252,11 +253,17 @@ AV::Sound::~Sound()
{
Sound_FreeSample(m_ss);
}
if (m_data != NULL)
{
delete[] m_data;
}
}
bool AV::Sound::load(unsigned char *data, unsigned int size)
{
m_rwops = SDL_RWFromMem(data, size);
m_data = new unsigned char[size];
memcpy(m_data, data, size);
m_rwops = SDL_RWFromMem(m_data, size);
Sound_AudioInfo desired;
desired.channels = CHANNELS;
desired.format = AUDIO_S16SYS;

View File

@ -38,6 +38,7 @@ class AV
void setVolume(float v);
protected:
AV & m_av;
unsigned char *m_data;
SDL_RWops * m_rwops;
Sound_Sample * m_ss;
int m_loop_count;

View File

@ -641,12 +641,10 @@ int Engine::loadSound(const string & name)
unsigned char *data = loadFile(nm.c_str(), &size);
if (data != NULL)
{
if (avs->load(data, size))
{
/* TODO: memory leak, fix with buffer */
return addSound(avs);
}
bool loaded = avs->load(data, size);
free(data);
if (loaded)
return addSound(avs);
}
cerr << "error loading sound '" << name << "'" << endl;
}