Hello good zig people!
I’m replicating most simple embree example, taken from here: API Examples
It works fine and well,
but
If I try to use an allocator to alloc a slice anywhere in the code (even in the end, in an unreachable branch) - embree call segfaults.
I’m new to ZIG, but this seems highly strange.
example is here:
const std = @import("std");
const print = std.debug.print;
const embree = @cImport({
@cInclude("embree3/rtcore.h");
});
const Err = error{general};
pub fn main() !void {
var device = embree.rtcNewDevice(null);
var scene = embree.rtcNewScene(device);
var geo = embree.rtcNewGeometry(device, embree.RTC_GEOMETRY_TYPE_TRIANGLE);
var vertex_buff: [*]f32 = @ptrCast(@alignCast(embree.rtcSetNewGeometryBuffer(geo, embree.RTC_BUFFER_TYPE_VERTEX, 0, embree.RTC_FORMAT_FLOAT3, 3 * @sizeOf(f32), 3) orelse {
return Err.general;
}));
vertex_buff[0..9].* = .{
0.0, 0.0, 0.0,
1.0, 0.0, 0.0,
0.0, 1.0, 0.0,
};
var index_buff: [*]u32 = @ptrCast(@alignCast(embree.rtcSetNewGeometryBuffer(geo, embree.RTC_BUFFER_TYPE_INDEX, 0, embree.RTC_FORMAT_UINT3, 3 * @sizeOf(u32), 1)));
index_buff[0..3].* = .{
0, 1, 2,
};
embree.rtcCommitGeometry(geo);
_ = embree.rtcAttachGeometry(scene, geo);
embree.rtcReleaseGeometry(geo);
embree.rtcCommitScene(scene);
var rayhit: embree.RTCRayHit = undefined;
rayhit.ray.org_x = 0;
rayhit.ray.org_y = 0;
rayhit.ray.org_z = -1;
rayhit.ray.dir_x = 0;
rayhit.ray.dir_y = 0;
rayhit.ray.dir_z = 1;
rayhit.ray.tnear = 0;
rayhit.ray.tfar = std.math.inf(f32);
rayhit.hit.geomID = embree.RTC_INVALID_GEOMETRY_ID;
var inter_context: embree.RTCIntersectContext = undefined;
embree.rtcInitIntersectContext(&inter_context);
embree.rtcIntersect1(scene, &inter_context, &rayhit);
if (rayhit.hit.geomID != embree.RTC_INVALID_GEOMETRY_ID) {
print("hit at distance {}", .{rayhit.ray.tfar});
} else {
print("no hit", .{});
}
// if (rayhit.hit.geomID == embree.RTC_INVALID_GEOMETRY_ID) {
// const alloc: std.mem.Allocator = std.heap.c_allocator;
// var data: []f32 = try alloc.alloc(f32, 0);
// defer alloc.free(data);
// }
}
this all is basically one-to-one embree example, with that commented extra code below. If you uncomment that - the call to rtcIntersect1
crashes with
Segmentation fault at address 0x0
???:?:?: 0x7f76a0268098 in ??? (libembree3.so.3)
Unwind information for `libembree3.so.3:0x7f76a0268098` was not available, trace may be incomplete
???:?:?: 0x0 in ??? (???)
Aborted (core dumped)
tried other allocators, such as page_allocator
- all same. also the size passed to alloc
does not make any difference, it crashes with size 0 as well as size 100k
using zig 0.11.0, embree 3.13.5
does anyone have any insight?