Skip to content

Commit 70892c8

Browse files
authored
Fix memory leak: free CString in helpers.go ValToDatum() initialization (#631)
1 parent 3d4fb50 commit 70892c8

1 file changed

Lines changed: 5 additions & 1 deletion

File tree

helpers.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,11 @@ func ValToDatum(val interface{}, cinfo *C.ConversionInfo, buffer C.StringInfo) (
154154
}
155155
}()
156156
// init an empty return result
157-
datum := C.fdw_cStringGetDatum(C.CString(""))
157+
// Allocate CString, use it, then immediately free to avoid memory leak
158+
// Using explicit C.free() instead of defer because this is a hot path
159+
emptyStr := C.CString("")
160+
datum := C.fdw_cStringGetDatum(emptyStr)
161+
C.free(unsafe.Pointer(emptyStr))
158162

159163
// write value into C buffer
160164
if err := valToBuffer(val, cinfo.atttypoid, buffer); err != nil {

0 commit comments

Comments
 (0)