fix leak
fix stack leak
optimize: reduce cgo calls