Skip to content

fixes: #345 InvokeMethodWithReply segfault #382

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 2, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions embedder/embedder.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ type FlutterOpenGLTexture struct {
type FlutterTask = C.FlutterTask

// FlutterEngine corresponds to the C.FlutterEngine with his associated callback's method.
//
// Trick the panic error:
// cgo argument has Go pointer to Go pointer
// GoType(KeepAlive) -> unsafe.Pointer -> uintptr(KeepAlive) -> unsafe.Pointer(Pass to the C function) <- uintptr <- unsafe.Pointer <- GoType
type FlutterEngine struct {
// Flutter Engine.
Engine C.FlutterEngine
Expand Down Expand Up @@ -346,17 +350,24 @@ func (flu *FlutterEngine) MarkExternalTextureFrameAvailable(textureID int64) Res

// DataCallback is a function called when a PlatformMessage response send back
// to the embedder.
type DataCallback func(binaryReply []byte)
//
// Trick the panic error:
// cgo argument has Go pointer to Go pointer
// GoType(KeepAlive) -> unsafe.Pointer -> uintptr(KeepAlive) -> unsafe.Pointer(Pass to the C function) <- uintptr <- unsafe.Pointer <- GoType
type DataCallback struct {
// Handle func
Handle func(binaryReply []byte)
}

// CreatePlatformMessageResponseHandle creates a platform message response
// handle that allows the embedder to set a native callback for a response to a
// message.
// Must be collected via `ReleasePlatformMessageResponseHandle` after the call
// to `SendPlatformMessage`.
func (flu *FlutterEngine) CreatePlatformMessageResponseHandle(callback DataCallback) (PlatformMessageResponseHandle, error) {
func (flu *FlutterEngine) CreatePlatformMessageResponseHandle(callback *DataCallback) (PlatformMessageResponseHandle, error) {
var responseHandle *C.FlutterPlatformMessageResponseHandle

callbackPointer := uintptr(unsafe.Pointer(&callback))
callbackPointer := uintptr(unsafe.Pointer(callback))
defer func() {
runtime.KeepAlive(callbackPointer)
}()
Expand Down
4 changes: 2 additions & 2 deletions embedder/embedder_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,6 @@ func proxy_post_task_callback(task C.FlutterTask, targetTimeNanos C.uint64_t, us
//export proxy_desktop_binary_reply
func proxy_desktop_binary_reply(data *C.uint8_t, dataSize C.size_t, userData unsafe.Pointer) {
callbackPointer := *(*uintptr)(userData)
handler := *(*DataCallback)(unsafe.Pointer(callbackPointer))
handler(C.GoBytes(unsafe.Pointer(data), C.int(dataSize)))
callback := (*DataCallback)(unsafe.Pointer(callbackPointer))
callback.Handle(C.GoBytes(unsafe.Pointer(data), C.int(dataSize)))
}
11 changes: 8 additions & 3 deletions messenger.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package flutter
import (
"errors"
"fmt"
"runtime"
"sync"

"github.com/go-flutter-desktop/go-flutter/embedder"
Expand Down Expand Up @@ -37,9 +38,13 @@ func newMessenger(engine *embedder.FlutterEngine) *messenger {
func (m *messenger) SendWithReply(channel string, binaryMessage []byte) (binaryReply []byte, err error) {
reply := make(chan []byte)
defer close(reply)
responseHandle, err := m.engine.CreatePlatformMessageResponseHandle(func(binaryMessage []byte) {
reply <- binaryMessage
})
callbackHandle := &embedder.DataCallback{
Handle: func(binaryMessage []byte) {
reply <- binaryMessage
},
}
defer runtime.KeepAlive(callbackHandle)
responseHandle, err := m.engine.CreatePlatformMessageResponseHandle(callbackHandle)
if err != nil {
return nil, err
}
Expand Down