Browse Source

Prevent memory errors on home page

master
Kenneth Bruen 9 months ago
parent
commit
61bcf53b33
Signed by: kbruen
GPG Key ID: C1980A470C3EE5B1
  1. 8
      src/home.zig
  2. 1
      src/main.zig
  3. 2
      src/state.zig

8
src/home.zig

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

1
src/main.zig

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

2
src/state.zig

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

Loading…
Cancel
Save