How to relase the memory allocated by c in golang

func SQLColAttribute(statementHandle SQLHSTMT, ColumnNumber SQLSMALLINT, FieldIdentifier SQLSMALLINT, CharacterAttributePtr SQLPOINTER, BufferLength SQLSMALLINT, StringLengthPtr *SQLSMALLINT, NumericAttributePtr *SQLLEN) (ret SQLRETURN) {
	r := C.SQLColAttribute(C.SQLHSTMT(statementHandle),C.SQLSMALLINT(ColumnNumber),C.SQLSMALLINT(FieldIdentifier),C.SQLPOINTER(CharacterAttributePtr),C.SQLSMALLINT(BufferLength),(*C.SQLSMALLINT)(StringLengthPtr),(*C.SQLLEN)(NumericAttributePtr))
	return SQLRETURN(r)
}

HERE:
CharacterAttributePtr — output
StringLengthPtr —output
NumericAttributePtr —output

I am using cgo to call the functions in cli driver. When i run the code i get goroutine problem i thing this is due to garbage collector unable to deallocate the memory allocated by the c. So how to solve this error.

Thanks.

The garbage collector can deallocate only Go pointers and basically the C code can do what it likes. I know that is not realy straightforward but i guess you can use a separate process to do this, procces that can be killed to free the memory.

Read below more details.

https://golang.org/cmd/cgo/#hdr-Passing_pointers

So that means In place of C.SQLPOINTER(CharacterAttributePtr) I have to write

C.free(unsafe.Pointer(CharacterAttributePtr)

Even better where to use the libraries functions to clean up.

A well designed library will provide you some functions to clean up after it. You have to take a look at the documentation of the library you are interfacing with.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.