Here is the example in our gorocksdb:

// Get returns the data associated with the key from the database.

func (db *DB) Get(opts *ReadOptions, key []byte) (*Slice, error) {

               var (

                              cErr    *C.char

                              cValLen C.size_t

                              cKey    = byteToChar(key)

               )

               cValue := C.rocksdb_get(db.c, opts.c, cKey, 
C.size_t(len(key)), &cValLen, &cErr)

               runtime.KeepAlive(key)  //<==this is the customization we 
added.

               if cErr != nil {

                              defer C.rocksdb_free(unsafe.Pointer(cErr))

                              return nil, errors.New(C.GoString(cErr))

               }

               return NewSlice(cValue, cValLen), nil

}

// byteToChar returns *C.char from byte slice.

func byteToChar(b []byte) *C.char {

               var c *C.char

               if len(b) > 0 {

                              c = (*C.char)(unsafe.Pointer(&b[0]))

               }

               return c

}
My question is: if the keepalive(key) is necessary above. Will the "key" be 
GC-ed when calling into C.rocksdb_get(). Does the compiler know there’s a 
reference to “key” (using unsafe.pointer) and not collect “key” when 
running GC?

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/a03805af-511d-400f-88b3-02e1501e3211n%40googlegroups.com.

Reply via email to