zeroshade commented on code in PR #38314:
URL: https://github.com/apache/arrow/pull/38314#discussion_r1364028113
##########
go/arrow/cdata/cdata.go:
##########
@@ -418,26 +409,41 @@ func (imp *cimporter) doImportChildren() error {
func (imp *cimporter) initarr() {
imp.arr = C.get_arr()
+ if imp.alloc == nil {
+ imp.alloc = &importAllocator{arr: imp.arr}
+ }
+}
+
+func (imp *cimporter) doImportArr(src *CArrowArray) error {
+ imp.arr = C.get_arr()
+ C.ArrowArrayMove(src, imp.arr)
+ if imp.alloc == nil {
+ imp.alloc = &importAllocator{arr: imp.arr}
+ }
+
+ // we tie the releasing of the array to when the buffers are
+ // cleaned up, so if there are no buffers that we've imported
+ // such as for a null array or a nested array with no bitmap
+ // and only null columns, then we can release the CArrowArray
+ // struct immediately after import, since we have no imported
+ // memory that we have to track the lifetime of.
+ defer func() {
+ if imp.alloc.bufCount == 0 {
+ C.ArrowArrayRelease(imp.arr)
+ }
+ }()
+
+ return imp.doImport()
}
// import is called recursively as needed for importing an array and its
children
// in order to generate array.Data objects
-func (imp *cimporter) doImport(src *CArrowArray) error {
- imp.initarr()
+func (imp *cimporter) doImport() error {
// move the array from the src object passed in to the one referenced by
// this importer. That way we can set up a finalizer on the created
// arrow.ArrayData object so we clean up our Array's memory when
garbage collected.
- C.ArrowArrayMove(src, imp.arr)
defer func(arr *CArrowArray) {
- if imp.data != nil {
- runtime.SetFinalizer(imp.data, func(arrow.ArrayData) {
- defer C.free(unsafe.Pointer(arr))
- C.ArrowArrayRelease(arr)
- if C.ArrowArrayIsReleased(arr) != 1 {
- panic("did not release C mem")
- }
- })
- } else {
+ if imp.data == nil {
Review Comment:
it should only happen on an error, yea. I'll add a comment
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]