Managing memory allocation when using Cgo
A common challenge when creating Cgo and Golang code that needs to manage dynamically created objects is memory management. How do you create an objet and ensure that the object isn’t leaked. The garbage collector will help manage natural native golang objects that are no longer needed once the scope is exited. But how can we do the same thing when we create dynamic memory objects with Cgo?
Turns out the answer lives at the intersection of
runtime.CG() and a wrapping up the objects in a golang struct.
Here’s an example of a golang program that allocates memory in C, and then frees it with a call to the finalizer:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
So what’s going on here? First we create a struct that will contain our dynmaically allocated memory, CFoo. Inside of CFoo I threw in a Mutex, a name, a allocCnt to allow us to track the allocations and releases of the memory, and lastly memory of type
unsafe.Pointer that will point to our dnyamically allocated object. The mutex and the count are soley illustrative and aren’t likely needed in practice.
Our CFoo struct contains a function
alloc that will do two important things:
- First it will allocated the memory via Cgo
- Next it will register the finalizer so that the memoy will be freed when the object leaves scope.
When we create the memory we assign it to our struct member. The object CFoo will exist until the context is no longer valid. And when it’s invalid, the finalizer will be called when the garbage collector is invoked.
`free(c *CFoo) function will take our CFoo and call the cgo
free() to release the memory.
It’s that simple!