#Help resolving a memory overwrite issue

1 messages · Page 1 of 1 (latest)

raven vessel
#

Hello ! I'm currently writing a renderer and I'm getting hardstuck on a memory issue.

In my init function of my renderer, I'm making a copy of the address of a struct

// init_default_data
self._default_data = self._metal_rough_material.write_material(self._device, material.MaterialPass.MainColor, &material_res, &_descriptor_pool);

std.debug.print("\nbefore insert\n\n", .{});

std.debug.print("opaque pipeline: {*}\n", .{self._default_data.pipeline.pipeline});

for (_test_meshes.items) |*mesh| {
  var new_node: *m.Node = try std.heap.page_allocator.create(m.Node);
  new_node.children = std.ArrayList(*m.Node).init(std.heap.page_allocator);
  new_node.mesh = mesh;

  new_node.local_transform = z.Mat4.identity().data;
  new_node.world_transform =  z.Mat4.identity().data;

  for (new_node.mesh.surfaces.items) |*s| {
    s.material = self._arena.allocator().create(loader.GLTFMaterial) catch @panic("OOM");
    s.material.data =  material.MaterialInstance{
      .pipeline = self._default_data.pipeline,
      .material_set = self._default_data.material_set,
      .pass_type = self._default_data.pass_type,
    };
    std.debug.print("pipeline address {*}\n", .{self._default_data.pipeline.pipeline});
    std.debug.print("ptr address {*}\n", .{self._default_data.pipeline });
}

// std.debug.print("name {s}, node {*}\n", .{ mesh.name, new_node });
self._loaded_nodes.append(.{ .key = mesh.name , .value = new_node }) catch @panic("OOM");
}```

I made sure the pipeline still exist after we have exited the init function but, for some reason, VkPipeline and VkPipelineLayout become instable/null once we exit renderer.init.
#

Here is the log, it might be clearer :

material pipeline: *cimport.struct_VkPipeline_T@ea7170000000031 // direct access
opaque pipeline: *cimport.struct_VkPipeline_T@ea7170000000031 // pipeline address from a copy
pipeline address *cimport.struct_VkPipeline_T@ea7170000000031 // pipeline address accessed from the ptr
ptr address renderer.material.MaterialPipeline@8783bfe950 // address to the object containing the pipeline (I'm storing it)

// back in main func after the init
material pipeline: *cimport.struct_VkPipeline_T@ea7170000000031 // direct access
opaque pipeline: *cimport.struct_VkPipeline_T@f // pipeline address from a copy
pipeline address *cimport.struct_VkPipeline_T@0 // pipeline address accessed from the ptr
ptr address renderer.material.MaterialPipeline@8783bfe950 // address to the object containing the pipeline (the one stored)``` As you can see, something seems to corrupt the memory, but I can't find it. If I remove the print for ```material pipeline: *cimport.struct_VkPipeline_T@ea7170000000031```, then the next one do show a value, but not a good one.
formal nimbus
#

it looks like you might be storing a pointer to self inside of self, but after returning from init, self changes addresses invalidating all of the previous pointers

raven vessel
#

Ok I fixed it, but I'm not so sure about the solution : that was what I was doing before

#
pub fn init(allocator: std.mem.Allocator, window: ?*c.SDL_Window, width: u32, height: u32) !renderer_t {
        var renderer = renderer_t{
            ._loaded_nodes = std.ArrayList(CHashMap).init(allocator),
            ._draw_context = m.DrawContext.init(allocator),
        };
        
        // init stuff for engine here

        return renderer;
    }```
#

Now I'm doing ```zig
pub fn init(allocator: std.mem.Allocator, window: ?*c.SDL_Window, width: u32, height: u32) !renderer_t {
var renderer = try allocator.create(renderer_t);
renderer.
= renderer_t{
._loaded_nodes = std.ArrayList(CHashMap).init(allocator),
._draw_context = m.DrawContext.init(allocator),
};

// init stuff here

return renderer;
}```

#

But following what you said, it seems weird that the data inside renderer_t is invalidated, so I fear I'm not fixing the real issue but getting around it. What do you think ?

#

And thanks for the tip, that was definitly it

sacred aspen
#

You could instead pass pointer to self in init and do initialization in place instead

var thing: Thing = undefined;
thing.init(...);
#

That way no need to allocate