I have seen it misused far too many times, so I'm in favour of deprecating it.
So instead of putting additional effort into finalizers and making
them better, I would consider making SetFinalizer a no-op (with a
timely announce), I believe this does not contradict any documentation
(because they may not run in any particular case already). And
removing SetFinalizer in Go2 entirely.
What do you think?
I see now that I've been largely misusing finalizers, I thought it was common practice to use them with cgo (I guess it is, just not a good practice) and had hope they would become more resilient (and.. not be removed). From what I understand here:
- Finalizers for freeing C things is not currently guaranteed and can effectively leak resources.
- All CGO users should (must?) switch to using manually free'd resources (ala 'defer f.Close()')
I do not understand the details of the Go runtime implementation, but #2 above IMO will be very harmful to the Go community as a whole when CGO is involved. Complex C hierarchies often require tricky memory management (especially for C objects that must remain for some given lifetime not easily determined, e.g. OpenGL or game engines), and right now we can use finalizers as a backup-way to free C things and offer explicit free'ing on-top of finalizers. I think to new people coming to the language it will be a weird and awkward thing ("Go is garbage collected unless C wrappers to anything are involved"), I'm not arguing that it doesn't make sense practically but I think it will be a turn-away for a lot of newcomers to Go.