#quick sanity check: seg fault on copied by value struct

1 messages · Page 1 of 1 (latest)

zenith scarab
#

what would cause a seg fault when accessing the fields of a struct that was copied by value

f.zig:98:25: 0x1784143 in resampleLight (a)
        q.ao.red = light.red;
                        ^
f.zig:219:44: 0x1752820 in setupLights (a)
            chunk.opaque_mesh.resampleLight();
             
pub fn resampleLight(self: *Mesh) void {
    for (self.quads.items) |*q| {
        const local_pos = int3.new(q.pos_and_model.x, q.pos_and_model.y, q.pos_and_model.z);
        const neighbour_local = local_pos.add(int3.getFaceDirection(q.pos_and_model.face));

        const light = self.parent_chunk.getLight(local_pos, neighbour_local);
        q.ao.red = light.red;
        q.ao.green = light.green;
        q.ao.blue = light.blue;
        q.ao.sunlight = light.sunlight;
    }

getLight returns a struct that is copied by value. the struct is a packed u32 with bit-compressed information
There is no multi threading.

pastel lion
#

my very low-confidence guess is that self.quads.items is invalid, or gets invalidated during the loop, causing a write to bad memory. it's really hard to tell just with this information.

can you share the code for the relevant type definitions (e.g. Mesh, int3, whatever light's type is) and functions (getLight)?

also also, if q.ao's type is the same as light's (and those four fields are the only ones) you can write

q.ao = light;
```instead of copying the fields one-by-one