I’m writing a binding for a C library and am wondering when I should be using ForeignPtr
instead of, say, withResource $ \resource -> ...
style resource allocation and deallocation?
It seems that ForeignPtr
is a handy thing to have, but it does move resource management to the GC. Couldn’t that lead to unintended resource exhaustion?
Are there guidelines on when to use it, and when not to?
1 Like
Based on touchForeignPtr
documentation it makes no sense to use it on any two resources with dependencies between them and the punishment for messing anything up is generally a segmentation fault.
With that in mind, you really would only ever want this when you have some independent resource that is both allocated and freed in C, and you want to pretend it’s pure. This is why StrictByteString
uses it under the hood (since those strings may be allocated outside of GC), and you’d probably use this if you were to port pcre2
regex-matching, for example.