srctree

Andrew Kelley parent e04df257 191c1c72
fix memory leaks when opening files

sometimes the FFmpeg functions take ownership of the input parameters, sometimes they don't. This makes correct memory management tricky. sure would be nice if they would stick to common C programming practices instead.
src/root.zig added: 13, removed: 5, total 8
@@ -55,9 +55,15 @@ pub const File = struct {
file.fs_file = try dir.openFile(sub_path, .{});
errdefer file.fs_file.close();
 
const buf = try av.malloc(4 * 1024);
const ioc = try av.IOContext.alloc(buf, .read_only, file, readPacket, writePacket, seek);
errdefer ioc.free();
const ioc = ioc: {
const buf = try av.malloc(4 * 1024);
errdefer av.free(buf.ptr);
break :ioc try av.IOContext.alloc(buf, .read_only, file, readPacket, writePacket, seek);
};
errdefer {
av.free(ioc.buffer);
ioc.free();
}
 
const fc = try av.FormatContext.open_input(filename_hint, null, null, ioc);
errdefer fc.close_input();
@@ -95,8 +101,10 @@ pub const File = struct {
pub fn close(f: *File) void {
f.decode.free();
f.fs_file.close();
av.free(f.fc.pb.?.buffer);
f.fc.pb.?.free();
f.fc.close_input();
f.* = undefined;
std.heap.raw_c_allocator.destroy(f);
}
 
pub fn audioFormat(f: File) AudioFormat {