Reputation: 981
I am confused as to if I need to cleanup the memory in the following scenario?
I have a C function that creates a C struct and passes it to a Go function. The C struct contains an array of values (using pointer arithmetic). The Go function populates this array and returns. In the calling C function I copy the values out of the C struct and do not store them.
As this is created in Go is this garbage collected?
/*
C code
*/
int go_func(c_struct *s);
struct c_struct{
val *values;
size_t *values_cnt;
};
void example_call()
{
struct c_struct s;
go_func(&s)
copy_values(s)
}
/*
go code
*/
func go_func(c *C.c_struct){
var varr *C.val
var v C.val = createValues()
C.set_val_in_array(varr, *v, C.size_t(0))
c.values = varr
}
Upvotes: 0
Views: 2252
Reputation: 173
Yes, it would definitely be garbage collected in Go as the memory is being created in Go.
Upvotes: 1