I have a pointer to a C type wrapped by a Go struct, like so:
type Wrapper struct {
unmanaged *C.my_c_type
}
The C type, in turn, has the following functions:
my_c_type* make_c_type();
void free_c_type(my_c_type *ct);
Is there a way that I can ensure that free_c_type is called whenever a Wrapper instance is finalized?
You can use runtime.SetFinalizer. This allows you to run a cleanup function when the object falls out of scope. It is not guaranteed to run. However, when freeing memory, that does not really matter. What does matter is that for a long running process, it is likely to keep the garbage in check.
Here are some excerpts from the docs (entire paragraphs were removed):