我从Python中调用Go函数。Go函数返回一个字符串,具体来说,返回一个GoString,该字符串本身在Go的一侧分配。
问题
谁要为这段记忆的删除负责?
下面是一个非常简单的例子。
站在一边:
func Create(optsEncoded string) (res string, serr string) {
opts := map[string]interface{}{}
if err := json.Unmarshal([]byte(optsEncoded), &opts); err != nil {
return "", errWithStack(err)
}
options := translateCreateOptions(opts)
result := ...
payload, err := json.Marshal(result)
if err != nil {
return "", errWithStack(err)
}
return string(payload), ""
}Cython绑定:
cpdef object py_create(object items, bytes options):
cdef GoString opts = ...
cdef bytes message
cdef Create_return result = Create(opts)
if result.r0.n == 0:
message = result.r1.p
raise Exception("Something happened")
message = result.r0.p
# Do I need to deallocate result.r0 and result.r1?
return message.decode("utf-8")https://stackoverflow.com/questions/48497879
复制相似问题