Fixed gltf parser memory leak

This commit is contained in:
Lorenzo Torres 2025-08-04 14:19:29 +02:00
parent a8071bd783
commit 59e3997056
2 changed files with 9 additions and 1 deletions

View file

@ -44,6 +44,8 @@ pub fn createVertexBuffer(allocator: Allocator, device: anytype) !vk.Buffer {
const gltf_data = try gltf.parseFile(allocator, "assets/models/block.glb");
const vertices = gltf_data.vertices;
defer allocator.free(vertices);
defer allocator.free(gltf_data.indices);
var data: [*c]?*anyopaque = null;
@ -77,6 +79,8 @@ pub fn createVertexBuffer(allocator: Allocator, device: anytype) !vk.Buffer {
pub fn createIndexBuffer(allocator: Allocator, device: anytype) !vk.Buffer {
const gltf_data = try gltf.parseFile(allocator, "assets/models/block.glb");
const indices = gltf_data.indices;
defer allocator.free(indices);
defer allocator.free(gltf_data.vertices);
//const indices = [_]u16{ 0, 1, 2, 3, 0, 2 };
var data: [*c]?*anyopaque = null;

View file

@ -152,9 +152,13 @@ pub const Model = struct {
pub fn parseFile(allocator: Allocator, name: []const u8) !struct { vertices: [][3]f32, indices: []u16 } {
const file = try std.fs.cwd().openFile(name, .{});
const all = try file.readToEndAlloc(allocator, 1_000_000);
defer allocator.free(all);
const json_chunk = std.mem.bytesAsValue(Model.Chunk, all[Model.Header.offset..]);
const data = (try std.json.parseFromSlice(Model.JsonChunk, allocator, @constCast(all[Model.Chunk.offset .. Model.Chunk.offset + json_chunk.length]), .{ .ignore_unknown_fields = true })).value;
const parsed = try std.json.parseFromSlice(Model.JsonChunk, allocator, @constCast(all[Model.Chunk.offset .. Model.Chunk.offset + json_chunk.length]), .{ .ignore_unknown_fields = true });
defer parsed.deinit();
const data = parsed.value;
const binary = Model.Binary{ .data = all[Model.Chunk.offset + json_chunk.length + 8 ..] };
const vertices = try binary.readVec3(allocator, data.bufferViews.?[data.meshes.?[0].primitives.?[0].attributes.?.POSITION.?], data.accessors.?[data.meshes.?[0].primitives.?[0].attributes.?.POSITION.?].count);