fix: allocate AppState on heap to prevent stack overflow
Co-authored-by: aider (deepseek/deepseek-coder) <aider@aider.chat>
This commit is contained in:
52
main.c
52
main.c
@@ -71,49 +71,51 @@ int main(int argc, char *argv[]) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create initial state
|
// Create initial state (heap-allocated to avoid stack overflow)
|
||||||
AppState initial_state;
|
AppState *initial_state = calloc(1, sizeof(AppState));
|
||||||
memset(&initial_state, 0, sizeof(AppState));
|
if (!initial_state) {
|
||||||
initial_state.transport_state = TRANSPORT_STOPPED;
|
fprintf(stderr, "Failed to allocate initial state\n");
|
||||||
initial_state.clock_source = CLOCK_SOURCE_INTERNAL;
|
return 1;
|
||||||
initial_state.bpm = 120.0;
|
}
|
||||||
initial_state.quantize_mode = QUANTIZE_OFF;
|
initial_state->transport_state = TRANSPORT_STOPPED;
|
||||||
initial_state.quantize_threshold = 0;
|
initial_state->clock_source = CLOCK_SOURCE_INTERNAL;
|
||||||
initial_state.running = true;
|
initial_state->bpm = 120.0;
|
||||||
|
initial_state->quantize_mode = QUANTIZE_OFF;
|
||||||
|
initial_state->quantize_threshold = 0;
|
||||||
|
initial_state->running = true;
|
||||||
|
|
||||||
for (int i = 0; i < MAX_CLIPS; i++) {
|
for (int i = 0; i < MAX_CLIPS; i++) {
|
||||||
initial_state.clips[i].state = CLIP_EMPTY;
|
initial_state->clips[i].state = CLIP_EMPTY;
|
||||||
initial_state.clips[i].buffer = (float *)calloc(MAX_BUFFER_SIZE, sizeof(float));
|
initial_state->clips[i].buffer = (float *)calloc(MAX_BUFFER_SIZE, sizeof(float));
|
||||||
if (!initial_state.clips[i].buffer) {
|
if (!initial_state->clips[i].buffer) {
|
||||||
fprintf(stderr, "Failed to allocate buffer for clip %d\n", i);
|
fprintf(stderr, "Failed to allocate buffer for clip %d\n", i);
|
||||||
// Cleanup previously allocated buffers
|
// Cleanup previously allocated buffers
|
||||||
for (int j = 0; j < i; j++) {
|
for (int j = 0; j < i; j++) {
|
||||||
free(initial_state.clips[j].buffer);
|
free(initial_state->clips[j].buffer);
|
||||||
}
|
}
|
||||||
|
free(initial_state);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
initial_state.clips[i].buffer_size = 0;
|
initial_state->clips[i].buffer_size = 0;
|
||||||
initial_state.clips[i].write_position = 0;
|
initial_state->clips[i].write_position = 0;
|
||||||
initial_state.clips[i].read_position = 0;
|
initial_state->clips[i].read_position = 0;
|
||||||
|
|
||||||
// Initialize MIDI clips
|
// Initialize MIDI clips
|
||||||
initial_state.midi_clips[i].state = CLIP_EMPTY;
|
initial_state->midi_clips[i].state = CLIP_EMPTY;
|
||||||
initial_state.midi_clips[i].event_count = 0;
|
initial_state->midi_clips[i].event_count = 0;
|
||||||
initial_state.midi_clips[i].read_index = 0;
|
initial_state->midi_clips[i].read_index = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Initialize channel names
|
// Initialize channel names
|
||||||
for (int ch = 0; ch < MAX_CHANNELS; ch++) {
|
for (int ch = 0; ch < MAX_CHANNELS; ch++) {
|
||||||
snprintf(initial_state.channel_names[ch], 64, "Channel %d", ch);
|
snprintf(initial_state->channel_names[ch], 64, "Channel %d", ch);
|
||||||
}
|
}
|
||||||
initial_state.show_midi_grid = false;
|
initial_state->show_midi_grid = false;
|
||||||
|
|
||||||
// Carla host is now initialized in engine_init
|
// Carla host is now initialized in engine_init
|
||||||
// Initialize dispatcher
|
// Initialize dispatcher
|
||||||
dispatch = dispatcher_init(&initial_state);
|
dispatch = dispatcher_init(initial_state);
|
||||||
|
free(initial_state);
|
||||||
// Free the heap-allocated initial state
|
|
||||||
// (dispatcher_init copies the state via memcpy)
|
|
||||||
|
|
||||||
// Initialize filesystem module (auto-save thread)
|
// Initialize filesystem module (auto-save thread)
|
||||||
fs_init(&initial_state);
|
fs_init(&initial_state);
|
||||||
|
|||||||
Reference in New Issue
Block a user