Skip to content

Commit 0c2542f

Browse files
committed
Fix crash when more than one sound emitter use one sound source
It crashed when one sound emitter was reading from sound source file and other emitter was destroying the handle of that file.
1 parent c06415e commit 0c2542f

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

src/xrSound/SoundRender_Source.cpp

+16-9
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,11 @@ bool ov_can_continue_read(long res)
5151

5252
void CSoundRender_Source::decompress(void* dest, u32 byte_offset, u32 size)
5353
{
54-
std::lock_guard guard{ read_lock };
55-
5654
if (!wave)
5755
attach();
5856

57+
std::lock_guard guard{ read_lock };
58+
5959
// seek
6060
const auto sample_offset = ogg_int64_t(byte_offset / m_wformat.nBlockAlign);
6161
const u32 cur_pos = u32(ov_pcm_tell(&ovf));
@@ -145,21 +145,28 @@ constexpr ov_callbacks g_ov_callbacks =
145145

146146
void CSoundRender_Source::attach()
147147
{
148-
VERIFY(0 == wave);
149-
if (wave)
150-
return;
151-
wave = FS.r_open(pname.c_str());
152-
R_ASSERT3(wave && wave->length(), "Can't open wave file:", pname.c_str());
153-
ov_open_callbacks(wave, &ovf, nullptr, 0, g_ov_callbacks);
148+
std::lock_guard guard{ read_lock };
149+
++refs;
150+
VERIFY(refs > 0);
151+
if (refs == 1)
152+
{
153+
wave = FS.r_open(pname.c_str());
154+
R_ASSERT3(wave && wave->length(), "Can't open wave file:", pname.c_str());
155+
ov_open_callbacks(wave, &ovf, nullptr, 0, g_ov_callbacks);
156+
}
154157
}
155158

156159
void CSoundRender_Source::detach()
157160
{
158-
if (wave)
161+
std::lock_guard guard{ read_lock };
162+
--refs;
163+
if (refs == 0)
159164
{
160165
ov_clear(&ovf);
161166
FS.r_close(wave);
162167
}
168+
if (refs < 0)
169+
refs = 0;
163170
}
164171

165172
bool CSoundRender_Source::LoadWave(pcstr pName, bool crashOnError)

src/xrSound/SoundRender_Source.h

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class XRSOUND_API CSoundRender_Source final : public CSound_source
1212

1313
OggVorbis_File ovf{};
1414
IReader* wave{};
15+
int refs{};
1516
std::mutex read_lock;
1617

1718
float fTimeTotal;

0 commit comments

Comments
 (0)