dptgpyl61857413 2016-03-31 13:06
浏览 63
已采纳

CGO中的垃圾收集

I have the following code in Go which uses cgo:

  func foo() {
       bar := new(C.struct_bar)
       ...
  }

Will bar be garbage collected after the function finishes its execution or I need to explicitly call free?

  • 写回答

1条回答 默认 最新

  • donglei1699 2016-03-31 13:26
    关注

    Yes, even though the struct_bar is a C type, the memory is allocated by Go, and will be collected by Go.

    Any memory allocated in C however is not tracked by the Go GC. The C code should manage this on it's own, but in cases like C.CString where Go triggers the allocation, you must manually free the memory with C.free.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?