Skip to content

Commit 59f576f

Browse files
committed
waver : fix data race with ggwave instance + v1.4.1
1 parent e2ef350 commit 59f576f

File tree

3 files changed

+23
-14
lines changed

3 files changed

+23
-14
lines changed

examples/ggwave-common-sdl2.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ SDL_AudioDeviceID g_devIdOut = 0;
2525
SDL_AudioSpec g_obtainedSpecInp;
2626
SDL_AudioSpec g_obtainedSpecOut;
2727

28-
GGWave *g_ggWave = nullptr;
28+
std::shared_ptr<GGWave> g_ggWave = nullptr;
2929

3030
}
3131

@@ -213,9 +213,7 @@ bool GGWave_init(
213213
}
214214

215215
if (reinit) {
216-
if (g_ggWave) delete g_ggWave;
217-
218-
g_ggWave = new GGWave({
216+
g_ggWave = std::make_shared<GGWave>(GGWave::Parameters {
219217
payloadLength,
220218
(float) g_obtainedSpecInp.freq,
221219
(float) g_obtainedSpecOut.freq,
@@ -228,7 +226,11 @@ bool GGWave_init(
228226
return true;
229227
}
230228

231-
GGWave *& GGWave_instance() { return g_ggWave; }
229+
std::shared_ptr<GGWave> GGWave_instance() { return g_ggWave; }
230+
231+
void GGWave_reset(void * parameters) {
232+
g_ggWave = std::make_shared<GGWave>(*(GGWave::Parameters *)(parameters));
233+
}
232234

233235
bool GGWave_mainLoop() {
234236
if (g_devIdInp == 0 && g_devIdOut == 0) {
@@ -278,8 +280,7 @@ bool GGWave_deinit() {
278280
return false;
279281
}
280282

281-
delete g_ggWave;
282-
g_ggWave = nullptr;
283+
g_ggWave.reset();
283284

284285
SDL_PauseAudioDevice(g_devIdInp, 1);
285286
SDL_CloseAudioDevice(g_devIdInp);

examples/ggwave-common-sdl2.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
#pragma once
22

33
#include <string>
4+
#include <memory>
45

56
class GGWave;
67

78
// GGWave helpers
89

910
void GGWave_setDefaultCaptureDeviceName(std::string name);
1011
bool GGWave_init(const int playbackId, const int captureId, const int payloadLength = -1, const float sampleRateOffset = 0);
11-
GGWave *& GGWave_instance();
12+
std::shared_ptr<GGWave> GGWave_instance();
13+
void GGWave_reset(void * parameters);
1214
bool GGWave_mainLoop();
1315
bool GGWave_deinit();

examples/waver/common.cpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -590,7 +590,11 @@ void updateCore() {
590590
static int rxDataLengthLast = 0;
591591
static float rxTimestampLast = 0.0f;
592592
static GGWave::TxRxData rxDataLast;
593-
static auto & ggWave = GGWave_instance();
593+
594+
auto ggWave = GGWave_instance();
595+
if (ggWave == nullptr) {
596+
return;
597+
}
594598

595599
{
596600
std::lock_guard<std::mutex> lock(g_buffer.mutex);
@@ -613,16 +617,18 @@ void updateCore() {
613617
GGWave::SampleFormat sampleFormatOutOld = ggWave->getSampleFormatOut();
614618
auto rxProtocolsOld = ggWave->getRxProtocols();
615619

616-
if (ggWave) delete ggWave;
617-
618-
ggWave = new GGWave({
620+
GGWave::Parameters parameters {
619621
inputCurrent.payloadLength,
620622
sampleRateInpOld,
621623
sampleRateOutOld + inputCurrent.sampleRateOffset,
622624
GGWave::kDefaultSamplesPerFrame,
623625
GGWave::kDefaultSoundMarkerThreshold,
624626
sampleFormatInpOld,
625-
sampleFormatOutOld});
627+
sampleFormatOutOld
628+
};
629+
630+
GGWave_reset(&parameters);
631+
ggWave = GGWave_instance();
626632

627633
ggWave->setRxProtocols(rxProtocolsOld);
628634
}
@@ -1016,7 +1022,7 @@ void renderMain() {
10161022

10171023
if (windowId == WindowId::Settings) {
10181024
ImGui::BeginChild("Settings:main", ImGui::GetContentRegionAvail(), true, ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoScrollWithMouse);
1019-
ImGui::Text("Waver v1.4.0");
1025+
ImGui::Text("Waver v1.4.1");
10201026
ImGui::Separator();
10211027

10221028
ImGui::Text("%s", "");

0 commit comments

Comments
 (0)