gogeos icon indicating copy to clipboard operation
gogeos copied to clipboard

Fixed a bug in encodeWkb() for Windows

Open andreacomparini opened this issue 9 years ago • 2 comments

Found a bug in encodeWkb() for windows. A buffer allocated in geos_c.dll whith malloc() cannot simply be freed with C.free(). Must use cGEOSFree(). Now all tests pass. The modified code follows:

func encodeWkb(e *wkbEncoder, g *Geometry, fn func(*C.GEOSWKBWriter, *C.GEOSGeometry, *C.size_t) *C.uchar) ([]byte, error) {
    var size C.size_t
    bytes := fn(e.w, g.g, &size)
    if bytes == nil {
        return nil, Error()
    }
    ptr := unsafe.Pointer(bytes)
    defer cGEOSFree((*C.void)(ptr))     //instead of C.free(ptr)

    l := int(size)
    var out []byte

    for i := 0; i < l; i++ {
        el := unsafe.Pointer(uintptr(ptr) + unsafe.Sizeof(C.uchar(0))*uintptr(i))
        out = append(out, byte(*(*C.uchar)(el)))
    }

    return out, nil 
} 

andreacomparini avatar May 24 '16 07:05 andreacomparini

Hi. Thanks for the solution. It really helped me. Btw, can u explain the reason for that?

l4d2boomer avatar Feb 13 '22 13:02 l4d2boomer

Hi. Thanks for the solution. It really helped me. Btw, can u explain the reason for that?

Hi. When you allocate memory in dll (win) with malloc you use a specific heap of dll, not a system wide one. So you must free the same memory using the same pointer local to dll code. The way to do this is to call a public entry point that calls free on the ptr of the specific heap from which it was allocated. GEOSFree is such a public method.

andreacomparini avatar Feb 15 '22 14:02 andreacomparini