Skip to content
This repository was archived by the owner on Feb 17, 2023. It is now read-only.

Commit e3e7803

Browse files
committed
Memory managed by Go can't be passed to C as of Go 1.6
Discussion at: golang/go#12416 https://github.com/golang/proposal/blob/master/design/12416-cgo-pointers.md Issues with the Go memory management code were hacked around by adding a new check that ensured that pointers to memory managed by Go are never passed to cgo code, since one can not be sure how and where pointers to Go memory are being stored in the C code. As a result, the memory must be manually malloc'd and free'd by cgo, even if it's a Go object in the C memory. The userdata fix makes this change explicit, by running a malloc and free using cgo, to explicitly control the memory management.
1 parent 7e9c756 commit e3e7803

File tree

2 files changed

+5
-1
lines changed

2 files changed

+5
-1
lines changed

webkit2/gasyncreadycallback.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ func newGAsyncReadyCallback(f interface{}) (cCallback C.GAsyncReadyCallback, use
2525
if rf.Kind() != reflect.Func {
2626
return nil, nil, errors.New("f is not a function")
2727
}
28-
cbinfo := &garCallback{rf}
28+
data := C.malloc(C.size_t(unsafe.Sizeof(garCallback{})))
29+
cbinfo := (*garCallback)(data)
30+
cbinfo.f = rf
2931
return C.GAsyncReadyCallback(C._gasyncreadycallback_call), C.gpointer(unsafe.Pointer(cbinfo)), nil
3032
}

webkit2/webview.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ func (v *WebView) RunJavaScript(script string, resultCallback func(result *gojs.
125125
var err error
126126
if resultCallback != nil {
127127
callback := func(result *C.GAsyncResult) {
128+
C.free(unsafe.Pointer(userData))
128129
var jserr *C.GError
129130
jsResult := C.webkit_web_view_run_javascript_finish(v.webView, result, &jserr)
130131
if jsResult == nil {
@@ -189,6 +190,7 @@ func (v *WebView) GetSnapshot(resultCallback func(result *image.RGBA, err error)
189190
var err error
190191
if resultCallback != nil {
191192
callback := func(result *C.GAsyncResult) {
193+
C.free(unsafe.Pointer(userData))
192194
var snapErr *C.GError
193195
snapResult := C.webkit_web_view_get_snapshot_finish(v.webView, result, &snapErr)
194196
if snapResult == nil {

0 commit comments

Comments
 (0)