You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Here, the C-heap-allocated (malloced) _cret pointer is converted into a pointer to a Go type, and a finalizer is set on the converted value. This doesn't work, however. Running this code will trigger a runtime panic:
fatal error: runtime.SetFinalizer: pointer not in allocated block
It seems like the most straightforward solution would be to forbid Go from allocating record types at all by generating all records like so:
This way, zero-values of Transform are always invalid, and the caller must use an appropriate constructor that mallocs the value. The nocopy field ensures that the struct value is never copied, because copying the struct value will ruin the finalizer. The alignment of that field means that nocopy does not grow the Transform type to be more than a pointer.
It might also be worth it to consider making native a uintptr type instead of an actual pointer type to reduce possible GC overhead, but this is likely already handled trivially in the Go runtime.
The text was updated successfully, but these errors were encountered:
An external package could cast the type without needing to access the unexported fields by unsafe casting the types, like how the code generator currently handles unexported fields from some cairo types:
Right now, record finalizers are implemented incorrectly. An example code from
./pkg/gsk/v4/gsktypes.go
shows this issue:Here, the C-heap-allocated (
malloc
ed)_cret
pointer is converted into a pointer to a Go type, and a finalizer is set on the converted value. This doesn't work, however. Running this code will trigger a runtime panic:It seems like the most straightforward solution would be to forbid Go from allocating record types at all by generating all records like so:
This way, zero-values of
Transform
are always invalid, and the caller must use an appropriate constructor thatmalloc
s the value. Thenocopy
field ensures that the struct value is never copied, because copying the struct value will ruin the finalizer. The alignment of that field means thatnocopy
does not grow theTransform
type to be more than a pointer.It might also be worth it to consider making
native
auintptr
type instead of an actual pointer type to reduce possible GC overhead, but this is likely already handled trivially in the Go runtime.The text was updated successfully, but these errors were encountered: