Skip to content
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

Do not free memory that needs to persist in the host #34

Closed
wants to merge 4 commits into from
Closed
Changes from all commits
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
8 changes: 7 additions & 1 deletion extism_pdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,9 @@ func OutputJSON(v any) error {
return err
}

OutputMemory(AllocateBytes(b))
mem := AllocateBytes(b)
defer mem.Free()
OutputMemory(mem)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we'd retain memory that needs to be accessed by the host elsewhere, shouldn't that also be true here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So my thought here was that the host would output the information immediately and the memory could be freed. That seemed to work in the MoonBit PDK but this is one reason why I think a suite of conformance tests would be fantastic... To help answer questions like this one.

return nil
}

Expand Down Expand Up @@ -149,6 +151,7 @@ func SetError(err error) {

func SetErrorString(err string) {
mem := AllocateString(err)
defer mem.Free()
extism_error_set(mem.offset)
}

Expand Down Expand Up @@ -190,6 +193,7 @@ func Log(level LogLevel, s string) {

func GetVar(key string) []byte {
mem := AllocateBytes([]byte(key))
defer mem.Free()

offset := extism_var_get(mem.offset)
clength := extism_length(offset)
Expand All @@ -215,6 +219,7 @@ func SetVar(key string, value []byte) {

func GetVarInt(key string) int {
mem := AllocateBytes([]byte(key))
defer mem.Free()

offset := extism_var_get(mem.offset)
clength := extism_length(offset)
Expand Down Expand Up @@ -243,6 +248,7 @@ func SetVarInt(key string, value int) {

func RemoveVar(key string) {
mem := AllocateBytes([]byte(key))
defer mem.Free()
extism_var_set(mem.offset, 0)
}

Expand Down