Looking for a little insight on if it is possible to do something:
Given this type:
type Blah struct {
Payload []byte
}
What I'm looking for is to kick off a finalizer when a slice and all other slices backed by the same array get GC'd.
In lieu of that, whenver the pointer to the array backing a slice get's GC'd. I realize that in this case, the slice actually may stay around when the array is GC'd because an append creates a new array on the slice.
I don't think the first is possible, as you can't call SetFinalizer() on non-pointer types. And I can't use any wrappers or pointers to []byte in lieu of []byte for my use case.
I figure there is a way to use:
hdr := (*reflect.SliceHeader)(unsafe.Pointer(&b.Payload))
to set a finalizer on the underlying arrray.
But I'm not sure exactly how to convert the hdr.Data into the specific array type pointer to use in SetFinalizer(). It may not be possible.
To save some time on a few questions: I'm aware of the flaws of SetFinalizer(), just looking to see if this can actually be done.
Thanks for any help.