Skip to content
This repository has been archived by the owner on Sep 19, 2023. It is now read-only.

Fixed a bug in encodeWkb() for Windows #15

Open
andreacomparini opened this issue May 24, 2016 · 2 comments
Open

Fixed a bug in encodeWkb() for Windows #15

andreacomparini opened this issue May 24, 2016 · 2 comments

Comments

@andreacomparini
Copy link

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 
} 
@l4d2boomer
Copy link

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

@andreacomparini
Copy link
Author

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.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants