Prevent memory errors on home page

This commit is contained in:
Kenneth Bruen 2024-02-23 13:11:02 +01:00
parent aa9656a089
commit 61bcf53b33
Signed by: kbruen
GPG key ID: C1980A470C3EE5B1
3 changed files with 12 additions and 1 deletions

View file

@ -72,6 +72,8 @@ fn fetchThread(state: *AppState) !void {
}
}
}
state.home_screen_state.mutex.lock();
defer state.home_screen_state.mutex.unlock();
if (state.home_screen_state.suggestions.len > 0) {
for (state.home_screen_state.suggestions) |suggestion| {
allocator.free(suggestion.id);
@ -94,8 +96,14 @@ pub fn render(state: *AppState) !void {
}
while (raylib.GetCharPressed()) |char| {
hs.station_name.appendAssumeCapacity(@intCast(char));
if (hs.station_name.items.len < hs.station_name_max_len) {
hs.station_name.appendAssumeCapacity(@intCast(char));
}
}
state.home_screen_state.mutex.lock();
defer state.home_screen_state.mutex.unlock();
while (raylib.GetKeyPressed()) |key| {
switch (key) {
rl.KEY_BACKSPACE => {

View file

@ -32,6 +32,7 @@ pub fn main() !void {
.ns_font = raylib.LoadFontEx("./private/ns.ttf", 64, cp, cp_cnt),
.home_screen_state = .{
.station_name = std.ArrayListUnmanaged(u8).initBuffer(&station_name_buffer),
.station_name_max_len = station_name_buffer.len - 1,
},
.departure_screen_state = .{
.platform = std.ArrayListUnmanaged(u8).initBuffer(&platform_buffer),

View file

@ -13,7 +13,9 @@ pub const HSSuggestion = struct {
};
pub const HomeScreenState = struct {
mutex: std.Thread.Mutex = .{},
station_name: std.ArrayListUnmanaged(u8),
station_name_max_len: usize,
fetch_thread: ?std.Thread = null,
suggestions: []HSSuggestion = &.{},
selection_idx: i8 = 0,