AV::Sound copies sound buffer to avoid memory leak
This commit is contained in:
parent
8f1329fed8
commit
278867b3c8
@ -244,6 +244,7 @@ AV::Sound::Sound(AV & av)
|
|||||||
m_ss = NULL;
|
m_ss = NULL;
|
||||||
m_buff_bytes_left = 0;
|
m_buff_bytes_left = 0;
|
||||||
m_volume = 1.0f;
|
m_volume = 1.0f;
|
||||||
|
m_data = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
AV::Sound::~Sound()
|
AV::Sound::~Sound()
|
||||||
@ -252,11 +253,17 @@ AV::Sound::~Sound()
|
|||||||
{
|
{
|
||||||
Sound_FreeSample(m_ss);
|
Sound_FreeSample(m_ss);
|
||||||
}
|
}
|
||||||
|
if (m_data != NULL)
|
||||||
|
{
|
||||||
|
delete[] m_data;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool AV::Sound::load(unsigned char *data, unsigned int size)
|
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;
|
Sound_AudioInfo desired;
|
||||||
desired.channels = CHANNELS;
|
desired.channels = CHANNELS;
|
||||||
desired.format = AUDIO_S16SYS;
|
desired.format = AUDIO_S16SYS;
|
||||||
|
1
src/AV.h
1
src/AV.h
@ -38,6 +38,7 @@ class AV
|
|||||||
void setVolume(float v);
|
void setVolume(float v);
|
||||||
protected:
|
protected:
|
||||||
AV & m_av;
|
AV & m_av;
|
||||||
|
unsigned char *m_data;
|
||||||
SDL_RWops * m_rwops;
|
SDL_RWops * m_rwops;
|
||||||
Sound_Sample * m_ss;
|
Sound_Sample * m_ss;
|
||||||
int m_loop_count;
|
int m_loop_count;
|
||||||
|
@ -641,12 +641,10 @@ int Engine::loadSound(const string & name)
|
|||||||
unsigned char *data = loadFile(nm.c_str(), &size);
|
unsigned char *data = loadFile(nm.c_str(), &size);
|
||||||
if (data != NULL)
|
if (data != NULL)
|
||||||
{
|
{
|
||||||
if (avs->load(data, size))
|
bool loaded = avs->load(data, size);
|
||||||
{
|
|
||||||
/* TODO: memory leak, fix with buffer */
|
|
||||||
return addSound(avs);
|
|
||||||
}
|
|
||||||
free(data);
|
free(data);
|
||||||
|
if (loaded)
|
||||||
|
return addSound(avs);
|
||||||
}
|
}
|
||||||
cerr << "error loading sound '" << name << "'" << endl;
|
cerr << "error loading sound '" << name << "'" << endl;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user