Hi all, 

Apologies if this has already been asked and answered elsewhere, I took a 
look and couldn't find anything.

I have a simple cgo program that looks a little something like:

func doTheCGo(){
    f := C.CString("/path/to/file.ext")
    o := C.CString("useful values for c call")
    defer func(){
        C.free(unsafe.Pointer(f))
        C.free(unsafe.Pointer(o))
    }()
    // call a C function with f and o; this function does NOT mutate this 
objects
}    

This results in a double free and then a stack underflow exception.

I have tried the code such that I do defer C.free(unsafe.Pointer(f) 
immediately after f is created, and the same for o. That works. I have 
tried freeing right at the end of my function, with no defer, and that 
works too. 

I understand that the convention is:
f := C.CString("/path/to/file.ext")
defer C.free(unsafe.Pointer(f))

I have now moved the code to that pattern. 

I have tried printing the addresses in a closure and they are the same as 
they are on creation, and the same as they are immediately prior to return.

What I am wondering is why the closure approach does not work?

Thanks
Nathan

-- 
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.

Reply via email to