propertly deinit the GPA so we can detect leaks in debug

This commit is contained in:
Mitchell Hashimoto
2022-10-25 15:49:21 -07:00
parent d8cdd5d8fe
commit e7ffb823af

View File

@ -20,7 +20,8 @@ pub fn main() !void {
log.info("dependency fontconfig={d}", .{fontconfig.version()}); log.info("dependency fontconfig={d}", .{fontconfig.version()});
} }
const gpa = gpa: { const GPA = std.heap.GeneralPurposeAllocator(.{});
var gpa: ?GPA = gpa: {
// Use the libc allocator if it is available beacuse it is WAY // Use the libc allocator if it is available beacuse it is WAY
// faster than GPA. We only do this in release modes so that we // faster than GPA. We only do this in release modes so that we
// can get easy memory leak detection in debug modes. // can get easy memory leak detection in debug modes.
@ -31,21 +32,28 @@ pub fn main() !void {
// We also use it if we can detect we're running under // We also use it if we can detect we're running under
// Valgrind since Valgrind only instruments the C allocator // Valgrind since Valgrind only instruments the C allocator
else => std.valgrind.runningOnValgrind() > 0, else => std.valgrind.runningOnValgrind() > 0,
}) break :gpa std.heap.c_allocator; }) break :gpa null;
} }
// We don't ever deinit our GPA because the process cleanup will break :gpa GPA{};
// clean it up. This defer isn't in the right location anyways because };
// it'll deinit on return from blk. defer if (gpa) |*value| {
// defer _ = general_purpose_allocator.deinit(); // We want to ensure that we deinit the GPA because this is
// the point at which it will output if there were safety violations.
var general_purpose_allocator = std.heap.GeneralPurposeAllocator(.{}){}; _ = value.deinit();
break :gpa general_purpose_allocator.allocator();
}; };
// If we're tracing, then wrap memory so we can trace allocations const alloc = alloc: {
const alloc = if (!tracy.enabled) gpa else alloc: { const base = if (gpa) |*value|
var tracy_alloc = tracy.allocator(gpa, null); value.allocator()
else if (builtin.link_libc)
std.heap.c_allocator
else
unreachable;
// If we're tracing, wrap the allocator
if (!tracy.enabled) break :alloc base;
var tracy_alloc = tracy.allocator(base, null);
break :alloc tracy_alloc.allocator(); break :alloc tracy_alloc.allocator();
}; };