Skip to content

Commit

Permalink
Fix memory leak in microphone feature
Browse files Browse the repository at this point in the history
  • Loading branch information
rexim committed Feb 24, 2024
1 parent 18b45e6 commit 50ea930
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions src/plug.c
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,8 @@ typedef struct {
#ifdef MUSIALIZER_MICROPHONE
// Microphone
bool capturing;
ma_device *microphone;
ma_device microphone;
bool microphone_ok;
#endif // MUSIALIZER_MICROPHONE
} Plug;

Expand Down Expand Up @@ -1296,25 +1297,25 @@ static void preview_screen(void)

#ifdef MUSIALIZER_MICROPHONE
if (IsKeyPressed(KEY_CAPTURE)) {
p->microphone_ok = true;
// TODO: let the user choose their mic
ma_device_config deviceConfig = ma_device_config_init(ma_device_type_capture);
deviceConfig.capture.format = ma_format_f32;
deviceConfig.capture.channels = 2;
deviceConfig.sampleRate = 44100;
deviceConfig.dataCallback = ma_callback;
deviceConfig.pUserData = NULL;
p->microphone = malloc(sizeof(ma_device));
assert(p->microphone != NULL && "Buy MORE RAM lol!!");
ma_result result = ma_device_init(NULL, &deviceConfig, p->microphone);
ma_result result = ma_device_init(NULL, &deviceConfig, &p->microphone);
if (result != MA_SUCCESS) {
TraceLog(LOG_ERROR,"MINIAUDIO: Failed to initialize capture device: %s",ma_result_description(result));
p->microphone_ok = false;
}
if (p->microphone != NULL) {
ma_result result = ma_device_start(p->microphone);
if (p->microphone_ok) {
ma_result result = ma_device_start(&p->microphone);
if (result != MA_SUCCESS) {
TraceLog(LOG_ERROR, "MINIAUDIO: Failed to start device: %s",ma_result_description(result));
ma_device_uninit(p->microphone);
p->microphone = NULL;
ma_device_uninit(&p->microphone);
p->microphone_ok = false;
}
}
p->capturing = true;
Expand Down Expand Up @@ -1450,10 +1451,10 @@ static void capture_screen(void)
int w = GetScreenWidth();
int h = GetScreenHeight();

if (p->microphone != NULL) {
if (p->microphone_ok) {
if (IsKeyPressed(KEY_CAPTURE) || IsKeyPressed(KEY_ESCAPE)) {
ma_device_uninit(p->microphone);
p->microphone = NULL;
ma_device_uninit(&p->microphone);
p->microphone_ok = false;
p->capturing = false;
}

Expand Down

0 comments on commit 50ea930

Please sign in to comment.