Skip to content

Commit

Permalink
FIX: Address Mac CGO Crash (wailsapp#3590)
Browse files Browse the repository at this point in the history
* Copy request to Go memory

* Update changelog.mdx

* Update v2/pkg/assetserver/webview/responsewriter_darwin.go

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>

* Fix import

---------

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
  • Loading branch information
2 people authored and therealsamf committed Aug 29, 2024
1 parent 9fb414c commit 5733a48
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
24 changes: 20 additions & 4 deletions v2/pkg/assetserver/webview/responsewriter_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ import "C"

import (
"encoding/json"
"fmt"
"net/http"
"unsafe"
)
Expand Down Expand Up @@ -98,16 +99,31 @@ func (rw *responseWriter) Write(buf []byte) (int, error) {

rw.WriteHeader(http.StatusOK)

var content unsafe.Pointer
var contentLen int
if buf != nil {
content = unsafe.Pointer(&buf[0])
contentLen = len(buf)
}

if !C.URLSchemeTaskDidReceiveData(rw.r.task, content, C.int(contentLen)) {
return 0, errRequestStopped
if contentLen > 0 {
// Create a C array to hold the data
cBuf := C.malloc(C.size_t(contentLen))
if cBuf == nil {
return 0, fmt.Errorf("memory allocation failed for %d bytes", contentLen)
}
defer C.free(cBuf)

// Copy the Go slice to the C array
C.memcpy(cBuf, unsafe.Pointer(&buf[0]), C.size_t(contentLen))

if !C.URLSchemeTaskDidReceiveData(rw.r.task, cBuf, C.int(contentLen)) {
return 0, errRequestStopped
}
} else {
if !C.URLSchemeTaskDidReceiveData(rw.r.task, nil, 0) {
return 0, errRequestStopped
}
}

return contentLen, nil
}

Expand Down
2 changes: 1 addition & 1 deletion website/src/pages/changelog.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]

### Fixed
- Fixed CGO memory issue on Darwin by @leaanthony in [PR](https://github.com/wailsapp/wails/pull/3590)
- Fixed an error that occurred when an author name contains a string that is not suitable for JSON. Fixed by @taiseiotsuka in [PR](https://github.com/wailsapp/wails/pull/3638)
- Fixed MacOS build to use `outputfilename` from wails.json. [#3200](https://github.com/wailsapp/wails/issues/3200)
- Fixed file drop events on windows. Fixed in [PR](https://github.com/wailsapp/wails/pull/3595) by @FrancescoLuzzi
Expand All @@ -24,7 +25,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
+ Modified `ZoomFactor` and `IsZoomControlEnabled` options to be Windows-only options in PR[#3644](https://github.com/wailsapp/wails/pull/3644) by @levinit
- Added nil check for Drag-n-Drop on Windows. Fixed by in [PR](https://github.com/wailsapp/wails/pull/3597) by @leaanthony based on the suggestion by @Alpa-1 in [#3596](https://github.com/wailsapp/wails/issues/3596).


## v2.9.1 - 2024-06-18

### Fixed
Expand Down

0 comments on commit 5733a48

Please sign in to comment.