The code to malloc, calloc and free in src/runtime/arch_tinygowasm.go does not ensure that malloc'ed memory is not freed during GC.
I experienced the gc=conservative freeing memory which was still used during the execution of a cgo function. The only option to avoid this was to use gc=leaking.
Not sure if this workaround has any undesired side-effects, but I could resolve the issue by keeping a reference to the alloced cgo pointers in Go.
Here is what I did:
- Add a map to the var (...) section of arch_tinygowasm.go:
cgoAllocations = make(map[uintptr]uintptr)
- Fill the map in libc_malloc and libc_calloc:
cgoAllocations[uintptr(allocResult)] = size
- Delete from the map in libc_free:
delete(cgoAllocations, uintptr(ptr))
The code to malloc, calloc and free in src/runtime/arch_tinygowasm.go does not ensure that malloc'ed memory is not freed during GC.
I experienced the gc=conservative freeing memory which was still used during the execution of a cgo function. The only option to avoid this was to use gc=leaking.
Not sure if this workaround has any undesired side-effects, but I could resolve the issue by keeping a reference to the alloced cgo pointers in Go.
Here is what I did:
cgoAllocations = make(map[uintptr]uintptr)cgoAllocations[uintptr(allocResult)] = sizedelete(cgoAllocations, uintptr(ptr))