Fix memory leaks and Generalize map key parsing in Go port - #862
Fix memory leaks and Generalize map key parsing in Go port#862roshankumar0036singh wants to merge 2 commits into
Conversation
| } | ||
|
|
||
| func valueDestroy(v unsafe.Pointer) { | ||
| if v == nil { |
There was a problem hiding this comment.
unless nil in Go is different from 0x0, then this is redundant
There was a problem hiding this comment.
you can directly use: C.metacall_value_destroy(v)
There was a problem hiding this comment.
updated it
|
|
||
| func TestMapNonStringKeys(t *testing.T) { | ||
| input := map[int]string{ | ||
| 1: "one", |
There was a problem hiding this comment.
if this is valid in Go, we can generalize it
There was a problem hiding this comment.
The fix i am planning (fmt.Sprintf("%v", key)) is generic, so it'll handle any key type, not just int
There was a problem hiding this comment.
you're going to convert any type to string then?
There was a problem hiding this comment.
there are two approaches i could think of that
1> Convert any non-string key with fmt.Sprintf("%v", key), keep returning map[string]interface{} it will not have any breaking changes but different key types can collide into the same string
2> Return an error (or change the function signature) instead of coercing, when a map has non-string keys but valueToGo doesn't currently return an error so this means changing its signature (breaking for callers) or panicking with a clearer message
There was a problem hiding this comment.
or we could inspect the key's reflect.Kind() construct map[string]interface{} when keys are strings, falling back to map[interface{}]interface{} (or some other generic form) only when they aren't That preserves the existing contract for the common path
There was a problem hiding this comment.
@viferga can you review the current two pass plan although its time consumer but as per your request of detecting the kesy at run time so one type => one type, else interface => interfacee does it holds well
231dccc to
33c5619
Compare
b87ea0c to
8e2bf27
Compare
8e2bf27 to
61fd5f5
Compare
Pull Request: Fix Memory Leaks in Go Port
Overview
This PR addresses several memory leaks related to
metacall_valueallocations in the Go Port, specifically during map creation and test assertions.Note on map keys: A regression test (
TestMapNonStringKeys) has been added to demonstrate a panic that occurs when the underlying engine (like Python) returns a dictionary with integer keys.Code Changes
1. Fix memory leak during Map conversion (
source/ports/go_port/source/go_port.go)Added
defer C.free(unsafe.Pointer(cArgs))to prevent leaking the C array of tuples when converting areflect.Map.2. Expose
valueDestroyutility (source/ports/go_port/source/go_port.go)Exposed a lightweight wrapper for cleaning up
metacall_valuepointers. Added anilpointer guard (if v == nil { return }) so this wrapper can be safely called defensively indeferstatements or after error paths where the pointer might not have been fully allocated.3. Prevent test values memory leak (
source/ports/go_port/source/go_port_test.go)Added the
valueDestroy(ptr)call at the end of theTestValuesloop to clean up the C values allocated bygoToValue(tt.input, &ptr).4. Added regression test for non-string map keys (
source/ports/go_port/source/go_port_test.go)Added
TestMapNonStringKeyswhich currently panics withinterface conversion: interface {} is int, not string. This test is included to highlight the issue for future resolution.