#How can I prevent segfault when hot reloading Zig code?
1 messages ยท Page 1 of 1 (latest)
This is my main.zig
The problem I'm referring to is in the readRadiusConfig function
the segfault behavior seems a little unpredictable
the ORIGINALLY COMPILED function is this:
fn readRadiusConfig(allocator: std.mem.Allocator) f32 {
const default_value: f32 = 10.0;
const config_data = std.fs.cwd().readFileAlloc(allocator, config_filepath, 1024 * 1024) catch {
std.debug.print("Failed to read {s}\n", .{config_filepath});
return default_value;
};
return std.fmt.parseFloat(f32, config_data[0 .. config_data.len - 1]) catch {
std.debug.print("Failed to parse {s}\n", .{config_filepath});
return default_value;
};
}
if I remove the file reading part and just return the default value like this:
fn readRadiusConfig(_: std.mem.Allocator) f32 {
const default_value: f32 = 10.0;
return default_value;
}
it DOES NOT segfault, hot reload works fine.
I can even add some more variables and still no segfault
fn readRadiusConfig(_: std.mem.Allocator) f32 {
const default_value: f32 = 10.0;
const new_value: f32 = 20;
return default_value + new_value;
}
But it will segfault if I do this:
fn readRadiusConfig(allocator: std.mem.Allocator) f32 {
const default_value: f32 = 10.0;
const new_value: f32 = 5.0; // add this
std.debug.print("{d}", .{new_value}); // add this
const config_data = std.fs.cwd().readFileAlloc(allocator, config_filepath, 1024 * 1024) catch {
std.debug.print("Failed to read {s}\n", .{config_filepath});
return default_value;
};
return std.fmt.parseFloat(f32, config_data[0 .. config_data.len - 1]) catch {
std.debug.print("Failed to parse {s}\n", .{config_filepath});
return default_value;
};
}
can someone please explain to me why is this happening? is there a way that I fix this somehow?
Thank you very much ๐
Or even this will segfault
fn readRadiusConfig(allocator: std.mem.Allocator) f32 {
const default_value: f32 = 10.0;
const config_data = std.fs.cwd().readFileAlloc(allocator, config_filepath, 1024 * 1024) catch {
std.debug.print("Failed to read {s}\n", .{config_filepath});
return default_value;
};
std.debug.print("{s}", .{config_data});
return default_value;
}
How can I prevent segfault when hot reloading Zig code?