ryoppippi / zigcv

zig bindings for OpenCV4
https://ryoppippi.github.io/zigcv
MIT License
100 stars 14 forks source link

Deallocate frame.toBytes() #54

Open Froxcey opened 4 hours ago

Froxcey commented 4 hours ago

In my code base, I'm calling mat.toByte() every frame Since that function allocates memory on heap, it causes memory leak Is there a way to do something like

var bytes = mat.toBytes();
defer mat.freeBytes();
Froxcey commented 3 hours ago

I found the fix, just need to do

pub fn toBytes(self: Self) []u8 {
    var p: c.struct_ByteArray = c.Mat_ToBytes(self.ptr);
+   defer c.free(p.data);

    const len = @as(usize, @intCast(p.length));
    return p.data[0..len];
}

in src/core/mat.zig

https://github.com/Froxcey/zigcv/commit/8076f266f97aa867f2c1dfc5e0465fe0cc4ce8d3