Hi, Test() never actually dereference the C string (now dangling) pointer after C.free. And even if it did, it is unlikely that the C.free() resulted in the memory page your string resided on was unmapped. Segfaults only happen if you access unmapped memory.
- elias On Friday, September 2, 2016 at 10:43:49 AM UTC+2, [email protected] wrote: > > Hello, > > I was expecting it to segfault after deallocating CString. How does it > work internally? > > func (m *Message) Test() string { > s := C.CString("test") > C.free(unsafe.Pointer(s)) > sh := reflect.StringHeader{(uintptr)(unsafe.Pointer(s)), 4} > return *(*string)(unsafe.Pointer(&sh)) > } > > -- 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 [email protected]. For more options, visit https://groups.google.com/d/optout.
