On Sat, Aug 19, 2017, 8:38 AM Ian Lance Taylor <i...@golang.org> wrote:
> On Fri, Aug 18, 2017 at 8:59 AM, Bhaskar Singhal > <bhaskar.sing...@gmail.com> wrote: > > > > C function just copies the parameters passed. All of the params have > escaped > > to heap as seen in output generated by using -gcflags "-m -m" during > build. > > > > I am trying to figure out if there is a possibility that gc frees any of > > these parameters before the C function call returns. > > Go's garbage collector never frees memory allocated by C.CString, just > as it never frees memory allocated by C.malloc. > > Ian > > > > On Fri, Aug 18, 2017 at 7:57 PM, Ian Lance Taylor <i...@golang.org> > wrote: > >> > >> On Fri, Aug 18, 2017 at 12:53 AM, Bhaskar Singhal > >> <bhaskar.sing...@gmail.com> wrote: > >> > I am running into a seg fault. The code keeps crashing either due to > >> > unexpected signal or double free. > >> > > >> > Any pointers on what I am doing wrong here: > >> > > >> > Code Snippet: > >> > // Put puts the given key / value to the kvstore > >> > func (kvs *kvstore) Put(key []byte, value []byte) error { > >> > /* Encode key to avoid null characters in byte[] */ > >> > strk := base64.StdEncoding.EncodeToString([]byte(key)) > >> > csk := C.CString(strk) > >> > defer C.free(unsafe.Pointer(csk)) > >> > > >> > /* Encode value to avoid null characters in byte[] */ > >> > strv := base64.StdEncoding.EncodeToString([]byte(value)) > >> > csv := C.CString(strv) > >> > defer C.free(unsafe.Pointer(csv)) > >> > > >> > if kvs.kvtree == nil { > >> > return fmt.Errorf("put failed because kvtree is nil") > >> > } > >> > size := C.int32_t(len(strv)) > >> > > >> > ret := C.put(kvs.kvtree, csk, csv, &size) > >> > if ret != C.int8_t(1) { > >> > return fmt.Errorf("kvtree put failed %g", ret) > >> > } > >> > return nil > >> > } > >> > > >> > C Function Put: > >> > int8_t put(KVTree* kv, > >> > const char* key, > >> > const char* value, > >> > const int32_t* valuebytes); > >> > >> > >> It really depends on how the C function behaves. My guess is that it > >> does not expect the values to be freed. > >> > >> Ian > > > > > I have cgo code that looks like this in a few of my own libs and I just started seeing crashes like this as of Go 1.8. Could it be related to the need for using runtime.KeepAlive? I have been adding that to my functions that look like this > -- > 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. > For more options, visit https://groups.google.com/d/optout. > -- 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. For more options, visit https://groups.google.com/d/optout.