3 ms·
The "C managed memory" problem is definitely ugly. Some libraries and APIs allow you to protect yourself from this by giving you the option to supply the memory
by nothrabannosir 11y ago
The "C managed memory" problem is definitely ugly. Some libraries and APIs allow you to protect yourself from this by giving you the option to supply the memory yourself, instead.
If your library can be used completely with you managing the memory, you can use Go's allocator. E.g. libopus:
size := C.opus_encoder_get_size(C.int(channels))
mem := make([]byte, size)
p := (*C.OpusEncoder)(unsafe.Pointer(&enc.mem[0]))
This way, all memory is on the Go heap.
Not every C library will support this, but those who do make CGo a lot more bearable.